2

I have a custom post type called Developments. They are buildings. They have two different taxonomies: City and Status. On the City taxonomy page for one of the buildings I'm trying to show all posts associated with the Status taxonomy.

<?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;
?>

This code outputs all of the Status terms, but it's showing all the posts too, I need it to show posts that are associated to City. I am not sure how to modify the code above to achieve this.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Darren Bachan
  • 735
  • 2
  • 11
  • 30

1 Answers1

1

Get your current taxonomy and term on the City taxonomy page like this:

$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

And then change you tax_query to add another array so that the current city taxonomy is added to the restrictions:

                       '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',
                            ),
                        ),
Amit Joshi
  • 1,334
  • 1
  • 8
  • 10