Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

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

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top