Exclude Taxonomy From WordPress Post Type

In my last post I mentioned how you can use the WordPress tax_query to exclude post-formats from your loops. We’ll you can also use the tax_query to easily exclude any custom taxonomy from a loop. For example if you created a custom post type called “Videos” with a “video category” taxonomy you can easily create a loop to exclude any video category.

Taking the same example above, lets say I want to show all the recent videos on my homepage except any videos that are posted under the “adult” category. So I’ll want to create my loop then add a tax query to exclude that whole category via it’s slug.

query_posts(array(
        'post_type'=>'videos',
        'tax_query' => array(
            array(
            'taxonomy' => 'video_cats',
            'field' => 'slug',
            'terms' => array( 'adult' ),
            'operator' => 'NOT IN',
            ),
        )
));
[block1] Where:

videos = custom post type
video_cats = my custom taxonomy
adult = taxonomy category to exclude

[/block1]

If you notice I’ve used the operator “NOT IN” because I want to show all the posts that aren’t a part of the adult category, you can also use the operator “IN” to show posts that are only in that category.

Also, you will see I’ve set up the terms as an array so you can also exclude multiple taxonomy categories as follows:

array('adult','action','adventure')
AJ Clarke | WPExplorer
Hey. My name is AJ and I am the boss around here. I own and operate WPExplorer. A website dedicated to everything I love about WordPress.
AJ Clarke | WPExplorer
AJ Clarke | WPExplorer

Add AJ Clarke | WPExplorer to your circles!

1 Comment

Leave a Reply