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.
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:
The solution to this isn’t really that publicised, but it should be.
You can do the following:
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:
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();
This code excludes status => sold from post_type => listings
I did the following to exclude posts from a certain taxonomy term:
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