WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a simple or easy way to exclude all posts from a custom taxonomy in the loop? I've been looking high and low, and neither SE, SO or Google seem to have a straight answer.

I know it can be done via a WPDB query, but that just seems like massive rope to jump for something that should be fairly simple.

share|improve this question
Any posts that have any terms in that taxonomy, or are you referring to a particular term of your custom taxonomy? Example: If my taxonomy is fruit and i have two terms, apple and orange, would i want to exclude all posts that have fruit, or just those that have apples? – t31os Mar 16 '11 at 15:00
to extend @t31os Q: post_tag, category, link_category, etc. are all _builtin taxonomies. So, even if you'd call post tags "terms", it's still only a taxonomy (non-hierarchical) and on the same "level" as "category" (hierarchical) taxonomy. – kaiser Mar 16 '11 at 17:36
To continue @t31os example: I am trying to exclude all posts that are in the taxonomy fruit (regardless of whether they're in 'apples', 'pears' or 'oranges'). – Thomas Mar 17 '11 at 7:52
up vote 7 down vote accepted

The solution to this isn't really that publicised, but it should be.

You can do the following:

$args['tax_query'] = array(
        'taxonomy' => 'category',
        'terms' => array('cat', 'dog'),
        'field' => 'slug',
        'operator' => 'NOT IN',

The operator argument can take other terms, but the above code is basically saying get all posts from the taxonomy 'category' that don't have the terms 'cat' or 'dog'.

share|improve this answer
Clever. But then I'd still need to manually enter every term in that taxonomy, correct? – Thomas Mar 21 '11 at 11:45
I'm not following you sorry. In the terms array, you define the term values that you want to exclude from your loop query. If you wanted them to be dynamic, you can perhaps add a custom meta value to each post and then check for the value of that meta key in your query and set it that way if that's what you mean. – Dwayne Charrington Mar 21 '11 at 12:18
Ah. I see what you mean, but that's really cumbersome. – Thomas Mar 27 '11 at 12:59
I haven't really looked, but there surely must be a plugin that does what you're asking then? – Dwayne Charrington Mar 27 '11 at 20:30

Here's how to do it for custom post types and custom taxonomies:

$happening = new WP_Query(
  'post_type'  => 'news',        // only query News post type
  'tax_query' => array(
        'taxonomy'  => 'news-cat',
        'field'     => 'slug',
        'terms'     => 'media', // exclude items media items in the news-cat custom taxonomy
        'operator'  => 'NOT IN')


This worked perfectly to exclude custom taxonomy from custom post type.. Just wanted to add the query loop code to finish off the snippet: while ( $the_query->have_posts() ) : $the_query->the_post();

share|improve this answer

I use the "RYO ‘Category Visibility’ WordPress Plugin" that seems to work pretty well.

You can decide on an admin page what categories are visible or excluded from various areas.



share|improve this answer
It seems to me that having to install an additional plugin to do this is over the top. I'd rather keep my dependencies to a minimum and surely there must be a way for this to be done in the loop. – Thomas Mar 17 '11 at 7:53

I did the following to exclude posts from a certain taxonomy term:

$argos = array( 
    'tax_query' =>  array (
            'taxonomy' => 'topics', // My Custom Taxonomy
            'terms' => 'college', // My Taxonomy Term that I wanted to exclude
            'field' => 'slug', // Whether I am passing term Slug or term ID
            'operator' => 'NOT IN', // Selection operator - use IN to include, NOT IN to exclude
    'post_type'=>'page', // Post type I want to show, can be a custom post type too
    'posts_per_page' => -1,
    'orderby' => 'title',
    'post_parent' =>$post->ID // show child posts or pages of current page/post

$query = new WP_Query( $args );
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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