Is it possible to exclude categories by slug in pre_get_posts? I can exclude categories by ID but it would be better if I can exclude posts by category name or slug.


Here's one way with the following tax query:

'tax_query' => [
        'taxonomy' => 'category',
        'field'    => 'slug',
        'terms'    => [ 'foo' ],
        'operator' => 'NOT IN'

where the foo term slug is excluded.

  • That was my problem. Had missing characters. Thanks! $query->set( 'tax_query', array(array('taxonomy' => 'category','field' => 'slug','terms' => array( 'podcast-control-daily' ),'operator'=> 'NOT IN'))); Sep 16 '17 at 23:44
  • Glad to hear it worked for you. Sometimes it's better to fetch what you want, instead of what you don't want ;-)
    – birgire
    Sep 16 '17 at 23:47

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.