Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
4  
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

4 Answers 4

up vote 2 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(
    array(
        'taxonomy' => 'category',
        'terms' => array('cat', 'dog'),
        'field' => 'slug',
        'operator' => 'NOT IN',
    ),
);
query_posts($args);

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. –  DigitalSea 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? –  DigitalSea Mar 27 '11 at 20:30

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

$happening = new WP_Query(
array( 
  'post_type'  => 'news',        // only query News post type
  'tax_query' => array(
    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.

http://ryowebsite.com/wp-plugins/category-visibility/

-Adam

share|improve this answer
1  
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 (
        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',
    'order'=>'ASC',
    'post_parent' =>$post->ID // show child posts or pages of current page/post
); 

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

Your Answer

 
discard

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.