Php 在“分类法”页面上,显示与该分类法和其他分类法关联的帖子

Php 在“分类法”页面上,显示与该分类法和其他分类法关联的帖子,php,taxonomy,term,taxonomy-terms,Php,Taxonomy,Term,Taxonomy Terms,我有一个自定义的帖子类型叫做Developments。它们是建筑物。他们有两种不同的分类法:城市和地位。在其中一栋建筑的城市分类页面上,我试图显示与状态分类相关的所有帖子 <?php // Vars $status_taxonomy = 'development_status'; $devs = get_terms( array( 'taxonomy' => $status_taxonomy, 'hide_empty' =>

我有一个自定义的帖子类型叫做Developments。它们是建筑物。他们有两种不同的分类法:城市和地位。在其中一栋建筑的城市分类页面上,我试图显示与状态分类相关的所有帖子

<?php
    // Vars
    $status_taxonomy = 'development_status';
    $devs = get_terms( array(
        'taxonomy' => $status_taxonomy,
        'hide_empty' => true,
    ) );
?>
<?php
    foreach($devs as $dev) : 
    $dev_id = $dev->term_id;
    $dev_name = $dev->name;
?>
<?php
    $term_slug = $dev->slug;
    $dev_posts = new WP_Query( array(
        'post_type'         => 'developments',
        'posts_per_page'    => -1, //important for a PHP memory limit warning
        'tax_query' => array(
            array(
                'taxonomy' => $status_taxonomy,
                'field'    => 'slug',
                'terms'    => $term_slug,
                'operator' => 'IN',
            ),
        ),
    ));

    if( $dev_posts->have_posts() ) :

        echo '<h3>'. $dev_name .'</h3>';
        while ( $dev_posts->have_posts() ) : $dev_posts->the_post();
        ?>
            <div class="col-xs-12">
                <h3>$dev_name</h3>
            </div>
            <div class="col-md-4">
                <h4><?php the_title(); ?></h4>
            </div>
        <?php
        endwhile;

    endif;
    wp_reset_postdata();
?>

<?php
    endforeach;
?>

$dev_name

这段代码输出了所有的状态项,但是它也显示了所有的帖子,我需要它来显示与城市相关的帖子。我不知道如何修改上面的代码来实现这一点。

在城市分类页面上获取您当前的分类法和术语,如下所示:

$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
然后将您的
tax\u query
更改为添加另一个数组,以便将当前城市分类添加到限制中:

                       'tax_query' => array
                        (
                            'relation' => 'AND',
                            array(
                                'taxonomy' => $status_taxonomy,
                                'field'    => 'slug',
                                'terms'    => $term_slug,
                                'operator' => 'IN',
                            ),
                            array(
                                'taxonomy' => 'city',
                                'field'    => 'slug',
                                'terms'    => $current_term->slug,
                                'operator' => 'IN',
                            ),
                        ),

非常感谢。这起作用了。我不想再添加第二个
税务查询