1

I'm trying to exclude a category from my tag template. Searching gives me nothing and kinda stuck. When I add the code below to my tag template it shows all posts... and I really would like to show all posts with a specific tag, but not the ones from category ID 433.

<?php $query = new WP_Query( array('cat' => -433, ) ); ?>
                        <?php if ($query->have_posts()) : ?>
                        <?php while ($query->have_posts()) : $query->the_post(); ?>

So, this is not working :) Also tried this one:

$query = new WP_Query( array( 'category__not_in' => array( 433 ) ) );

Also showing all posts. Hope anyone got an idea.

2

Have you tried using a normal loop in your tag template like this

<?php while ( have_posts() ) : the_post() ?>
    //add content code or template
<?php endwhile; ?>

And then using the pre_get_posts filter like this

function exclude_category( $query ) {
    if ( $query->is_tag() && $query->is_main_query() ) {
        $query->set( 'cat', '-433' );
    }
}
add_action( 'pre_get_posts', 'exclude_category' );
  • never checked the pre_get_posts filter. Works like a charm! Many thanks Stims! – AKNL Oct 14 '16 at 12:31
  • Glad to hear, pre_get_posts is clutch! – stims Oct 14 '16 at 13:06

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.