I’ve got the following query on my page template:
$args = array( 'post_type' => 'givingback', 'post_status' => 'publish', 'posts_per_page' => 100, 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'givingback_details_funds', 'compare' => 'EXISTS', ), array( 'key' => 'givingback_details_days', 'compare' => 'EXISTS', ) ) ); $the_query = new WP_Query( $args );
But on the taxonomy (category) pages we need to add a taxonomy into the mix so I added:
$page_slug = basename( get_permalink() ); $args = array( 'post_type' => 'givingback', 'posts_per_page' => 100, 'gbdealership_category' => $page_slug, /** etc... **/ );
Posts are filtered by taxonomy. If the taxonomy is empty, instead of showing an empty page, all the posts show.
If the taxonomy is empty, the posts that incorrectly appear on the page use the correct template.
Adding the following to
$args filters posts correctly (even when taxonomy is empty), but posts still always use 'if no posts are shown' template:
'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'month_category', 'field' => 'slug', 'terms' => $page_slug, ), ),
I struggle with PHP so I'm sure it's something obvious I'm doing wrong.