首页 > 解决方案 > WP循环,按自定义分类法分组,也仅使用帖子的主要分类法

问题描述

我有一个位于某个区域的场地循环。

我按类别(酒吧、餐馆等)对它们进行分组。

问题是我的一些场地有 2 个或更多类别。其中一个预计是主要的,但在我的循环中,它们出现的次数与所选类别的数量一样多。

这是我的代码:

<?php foreach ($venue_categories as $category):

                            $category_venues = new WP_Query([
                                'post_type' => 'venues',
                                'post_status' => 'publish',
                                'posts_per_page' => -1,
                                'paged' => false,
                                'orderby' => 'rand',
                                'order' => 'DESC',

                                'tax_query' => [
                                    [
                                        'taxonomy' => 'venues_tax',
                                        'terms' => $category->term_id,
                                        'field' => 'term_id'
                                    ],
                                ],
                                
                                'meta_query' => [
                                    [
                                        'key' => 'venue_is_closed',
                                        'value' => 0
                                    ],
                                    [
                                        'key' => 'district',
                                        'value' => $districtid
                                    ],

                                ],

                                ]);

                            if ($category_venues->have_posts()): ?>

                                <div class="col-12">
                                    <h4>
                                        <?php echo $category->name ?>
                                    </h4>
                                </div>

                                <? while ($category_venues->have_posts()):

                                    $category_venues->the_post();
                                    get_template_part('template-parts/single/venue-list', 'round-third');
                                
                                endwhile;
                                
                                wp_reset_postdata();

                            endif;
                            
                        endforeach; ?>

问题很简单——如何在我的循环中只使用主要类别?

标签: wordpressloopscustom-wordpress-pages

解决方案


推荐阅读