1

I'd like to exclude Tag ID: 26 from my homepage. I've figured out how to do it with categories, but not with tags.

function exclude_posts( $query ) { 
if ( $query->is_home ) {  $query->set( 'cat', '-13' ); 
} } add_action( 'pre_get_posts', 'exclude_posts' );

1 Answer 1

2

This should work

function exclude_posts( $query ) { 

    if ( $query->is_home() ) {  

        $query->set( 'tag__not_in', array( 13 ) ); 

    } 

} 

add_action( 'pre_get_posts', 'exclude_posts' );

tag and tag_id are not set up like cat to accept negative numbers (ID numbers) for exclusion (conclusion based on experiments, not digging into the code (some other time, perhaps!)). Also, all of the examples in the Codex use is_home(), is_admin(), etc., but apparently is_home does work - without the ().

2
  • Hi CK, your code worked. Thanks for sharing it as well as the explanation! Oct 31, 2017 at 16:25
  • You're welcome, but please don't forget to mark my answer as good/accepted - as I close in on the magic 1000 mark!
    – CK MacLeod
    Oct 31, 2017 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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