Wordpress - How do I exclude a custom taxonomy from the post loop

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


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

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