0

I'm running into a particular problem with an archive page. This particular page is supposed to list all post that belong to a category, but is able to be filtered by a second (custom) taxonomy: i.e.: posts that belong to both 'News' and 'Entertainment' (where the latter belongs to 'Topic' taxonomy).

The problem is that the filter link should not appear if there are no posts that belong to both taxonomy terms, but so far I haven't been able to remove the "empty" links as the query is being overriden by the main one -- here's some code to explain:

if ( is_single() ) {
    $cats =  get_the_category();
    $cat = $cats[0];
} else {
    $cat = get_category( get_query_var( 'cat' ) );
}

$topics = get_terms( array(
        'taxonomy' => 'topic',
        'hide_empty' => true, // thought this would do the trick, but no.
) );

$topic_query_args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'nopaging' => true,
);

foreach( $topics as $topic ):
    // This could get really slow. Refactor when able.
    $topic_tax_query = array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => $cat->slug
        ),
        array(
            'taxonomy' => 'topic',
            'field' => 'slug',
            'terms' => $topic->slug
        )
    );

    // Reassign the tax_query for the new $topic->slug:
    $topic_query_args['tax_query'] = $topic_tax_query;
    // Run secondary query:
    $topic_query = new WP_Query( $topic_query_args );
    $found_posts = $topic_query->found_posts;
    echo '<!--'. print_r( $topic_query, true ) .'-->'.PHP_EOL;
    // wp_reset_postdata(); // Probably not needed, as I'm not running $topic_query->the_post()

    // Here's the problem -- secondary query returns the same number of posts for all secondary terms:
    echo '<!--Found posts for '. $topic->slug .': '. $found_posts .'-->'. PHP_EOL;

    if ( $topic_query->found_posts == 0 ) {
        // Skip printing the link:
        continue;
    } else {
        // Process and print the link here.
        // ...extra code goes here...
    }
endforeach; 

If I run this with no filters (i.e.: example.com/category/news), it works as expected, the empty filter links do not appear. However, once I click on one of the links (resulting in example.com/category/news/?topic=entertainment) I get all of the filter links again, and the "print_r( $topic_query, true )" returns the main query_vars and tax_query instead of the secondary query's.

I apologize for the long-winded explanation, but I've been at this for a while now and am coming up with nothing. Any ideas?

1

I believe the error lies in

$topic_query['tax_query'] = $topic_tax_query;

which should be

$topic_query_args['tax_query'] = $topic_tax_query;

A simple variable mixup, so your taxonomy query makes it into WP_Query.

  • Thanks for your reply; it was actually a transcription/cleanup error (edited the question to reflect that). The taxonomy query is indeed making it into WP_Query as you state, but the actual query does not reflect it. – kenshin23 Nov 8 '17 at 14:27
  • Any filters on pre_get_posts? – janh Nov 8 '17 at 14:55
  • Now that you mention it, I did a search in the theme's function.php file and found a blanket filter for all categories if a specific $_GET variable is set. I also included a filter per a related question but am running into integration issues, i.e.: it works, but breaks other areas of the site. Maybe the first filter is why. – kenshin23 Nov 9 '17 at 16:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.