1

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.

  • Since the question is primarily about the query returning empty results or not, I've removed the references to your templates along with The Loop code. If you feel that this information is completely necessary to your question, feel free to roll-back my edit. – Howdy_McGee Mar 1 '18 at 16:22
  • please verify the meta keys did exist in post meta. for example adding a custom fields metabox to a post doesn't mean all posts well have an empty meta value for the field added. otherwise are you using tax query AND gbdealership_category at the same time? – Joel M Mar 2 '18 at 2:08
  • please see this answer.. diff question but i think the answer will be helpful. wordpress.stackexchange.com/a/295039/93297 – Joel M Mar 2 '18 at 2:13
  • The annoying thing is all I need to do is show the word "and" between givingback_details_funds and givingback_details_days if there is a value for both. I'm sure there is a better way to do it but this is as far as I can get. – user6744245 Mar 2 '18 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.