Executing Queries in tag.php

That’s because the main query is being discarded and replaced with your custom query. You’ve not told your custom query to look for that tag, so why would it? You might also notice your pagination is broken for the same reason, you’ve not told the new query which page you’re on, so why would it pull the right page?

What’s more, that main query is expensive! Why are you throwing it away! Why not modify it instead?

Imagine you sent your friend to get you a latte every morning, and every day after a 20 minute trip, you threw the coffee in the bin and said “I’m lactose intolerant, give me a hot chocolate instead”. They then go off for another 20 minute trip to fetch the hot chocolate, leaving your friend annoyed, and you waiting 40 minutes instead of 20. Wouldn’t it be easier if you tapped them on the shoulder before they left and said “Hey I changed my mind, I want a hot chocolate”?

Use the pre_get_posts filter to change the options on the main query before it happens, e.g. here’s how you can change how many posts are per page:

add_filter( 'pre_get_posts', function( \WP_Query $query ) {
    if ( $query->is_tag() && $query->is_main_query() ) {
        $query->set( 'posts_per_page', 5 );
    }
} );

You can use $query->set to change other options too, and you should do this to set your post types and taxonomy exclusion.

A final note on data storage and performance

You should never look for everything but XYZ, you want to say what you’re looking for, not what you’re not looking for. It’s slower, and doesn’t scale. Sometimes it’s actually faster to get everything then manually remove it in PHP!

So instead of adding posts to an archived term, consider using an unread/unarchived term, and looking for those instead?

Leave a Comment