Remove Taxonomy from WP Post Type

In a previous post, we discussed how the WordPress tax_query can be used to exclude post-formats from loops. However, this powerful tool can also be used to exclude custom taxonomies from loops. For instance, if you have created a custom post type called “Videos” with a “video category” taxonomy, you can easily create a loop that excludes any video category.

To illustrate this point, let’s assume that you want to display all recent videos on your homepage except for those posted under the “adult” category. In this case, you would need to create a loop and then add a tax query to exclude the entire category using its slug.

Here is an example of how you can achieve this:

query_posts( array(‘post_type’ =>’videos’,’tax_query’ => array(array(‘taxonomy’ => ‘video_cats’,’field’ => ‘slug’,’terms’ => array( ‘adult’ ),’operator’ => ‘NOT IN’,),)) );

In the above code, “videos” refers to the custom post type, “video_cats” refers to the custom taxonomy, and “adult” is the taxonomy category that you want to exclude.

Note that the operator “NOT IN” is used because you want to show all posts that are not part of the adult category. You can also use the operator “IN” to display posts that are only in that category.

Additionally, you can exclude multiple taxonomy categories by setting up the terms as an array. For example:

array(‘adult’,’action’,’adventure’);

By using the tax_query in this way, you can easily customize your loops to display only the content that you want. This is particularly useful when you have a large amount of content on your site and want to filter it based on specific criteria.

However, it’s important to note that using query_posts can have some negative effects on your site’s performance. This function overrides the main query and can cause issues with pagination, caching, and other features. Therefore, it’s recommended that you use WP_Query or get_posts instead.

In conclusion, the tax_query is a powerful tool that can be used to exclude custom taxonomies from loops in WordPress. By using this feature, you can easily customize your site’s content and display only the posts that meet your specific criteria. However, it’s important to use this function carefully and consider its impact on your site’s performance.

Stay in Touch

spot_img

Related Articles