Querying CPT with Two Taxonomies

I’ve been trying to sort this out for a while with no luck so maybe someone can point me in the right direction.

I created a CPT called reports and have two custom Taxonomies under it called report-locations and report-weeks. I want a page that only queries reports with both report-weeks and report-locations.

The reports will be listed under each report-weeks and then under that Week they should list the report-locations and I’d like to be able to order them somehow.

So far I have the following which gets me close but it relies on a secondary function to grab and list the report-locations and therefore doesn’t allow me to order those posts since they aren’t part of the 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; ?>

Read more here:: Querying CPT with Two Taxonomies

Leave a Reply

Your email address will not be published. Required fields are marked *