22

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.

  • 5
    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
13

You would want to use the NOT EXISTS operator along with passing the taxonomy slug, which tells the query not to include any of a chosen category from your custom taxonomy inside the loop.

To exclude all posts that are in the taxonomy "fruit" (regardless of fruit kind), here is the snippet:

$args = array(
    'post_type'      => 'post',
    'tax_query'      => array(
        array(
            'taxonomy' => 'fruit',
            'operator' => 'NOT EXISTS'
        )
    )
);

$query = new WP_Query( $args );
  • 2
    This looks like it should be more efficient than saying NOT IN XX number of terms. That list of terms could grow to a large size, but this should cover all terms with that taxonomy's name. – italiansoda May 12 '17 at 19:48
  • While this is the answer to the OP's question (upvoted), most of us probably want to exclude a specific term or set of terms. For that, you want this answer. – rinogo Aug 2 at 18:34
26

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'.

  • 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
8

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();

0

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 );
0
query_posts( array(
    'post_type' => 'listings',
    'tax_query' => array(
        array(
            'taxonomy' => 'status',
            'field'    => 'slug',
            'terms'    => 'sold',
            'operator' => 'NOT IN'
            ),
        )
    )
);

This code excludes status => sold from post_type => listings

-1

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

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

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.