使用两个分类法查询CPT

时间:2018-06-08 作者:tylorreimer

我一直在努力解决这个问题,但运气不好,所以也许有人能给我指出正确的方向。

enter image description here

我创建了一个名为reports 下面有两个自定义分类法report-locationsreport-weeks. 我想要一个只有查询的页面reports 两者都有report-weeksreport-locations.

这个reports 将列在每个report-weeks 然后在那周他们应该列出report-locations 我希望能够以某种方式订购它们。

到目前为止,我已经完成了以下内容,但它依赖于一个辅助函数来获取和列出report-locations 因此不允许我订购这些帖子,因为它们不是WP_Query.

<?php
$post_type = \'reports\';
$weeks = get_terms( array(
    \'taxonomy\' => \'report-weeks\',
    \'orderby\' => \'name\',
    \'order\' => \'DESC\'
) );

foreach ( $weeks as $week ) :

    $cat_query = new WP_Query( array(
        \'post_type\' => $post_type,
        \'tax_query\' => array(
            array(
                \'taxonomy\' => $week->taxonomy,
                \'field\' => $week->slug,
                \'terms\' => $week->term_id,
            ),
        ),
        \'posts_per_page\' => \'-1\',
    ) );

    if ( $cat_query->have_posts() ) : ?>
<h2><?php echo $week->name; ?></h2>

<?php while ( $cat_query->have_posts() ) : $cat_query->the_post(); ?>
<article id="<?php echo \'report-\' . $post->ID; ?>">
    <h3><?php list_custom_taxonomy(\'report-locations\', $post->ID); ?></h3>
<div><?php the_content(); ?></div>
</article>
<?php endwhile; wp_reset_postdata(); ?>
<?php endif; endforeach; ?>

1 个回复
SO网友:Krzysiek Dróżdż

免责声明:这不是一个非常有效的解决方案,我希望您在这两个分类法中没有太多术语(如果有,那么您应该做得稍微不同,并包括一些自定义SQL查询)。

好的,免责声明完成,我们可以转到解决方案;)下面是:

<?php
    $post_type = \'reports\';
    $weeks = get_terms( array(
        \'taxonomy\' => \'report-weeks\',
        \'orderby\' => \'name\',
        \'order\' => \'DESC\'
    ) );
    $locations = get_terms( array(
        \'taxonomy\' => \'report-locations\',
        \'orderby\' => \'name\',
        \'order\' => \'ASC\'
    ) );
    $results = array();

    foreach ( $weeks as $week ) {
        $week_results = array();
        foreach ( $locations as $location ) {
            $posts = new WP_Query( array(
                \'post_type\' => $post_type,
                \'tax_query\' => array(
                    array(
                        \'taxonomy\' => $week->taxonomy,
                        \'terms\' => $week->term_id,
                    ),
                    array(
                        \'taxonomy\' => $location->taxonomy,
                        \'terms\' => $location->term_id,
                    ),
                ),
                \'posts_per_page\' => \'-1\',
            ) );

            if ( $posts->have_posts() ) {
                $week_results[ $location->term_id ] = $posts;
            }
        }
        if ( ! empty( $week_results ) ) {
            $results[$week->term_id] = $week_results;
        }
    }

    foreach ( $weeks as $week ) :
        if ( ! array_key_exists( $week->term_id, $results ) ) continue;
    ?>
        <h2><?php echo $week->name ?></h2>
        <?php
            foreach ( $locations as $location ) :
                if ( ! array_key_exists( $location->term_id, $results[$week->term_id] ) ) continue;

                $query = $results[$week->term_id][$location->term];
        ?>
            <h3><?php echo $location->name; ?></h3>
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                <h4><?php the_title(); ?></h4>
                <div><?php the_content(); ?></div>
            <?php endwhile; ?>
        <?php endforeach; ?>
<?php
    endforeach;
    wp_reset_postdata();
?>
那么我们在那里做什么呢?首先,我们每周和每个地点进行迭代,并准备WP_Query 每双鞋都有。这样,我们就可以省略没有任何帖子分配给它们的对。

准备好这些查询后,我们可以输出结果。因此,我们必须再次迭代并打印分配给给定对的帖子。

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post