Check if Product Belongs to a Product Category or Tag

Are you trying to make it work with in_category() function but nothing happens? 🤔 Well in this tutorial you will find out why.

Actually it is a very common mistake and I also did it before.

First things first, let’s begin with the fact that products in WooCommerce are a custom post type product and product categories are a custom taxonomy product_cat. When you work with custom taxonomies, do you use functions for categories/tags or for taxonomies? That’s it 😁

Comparing to is_product_category(), WooCommerce doesn’t have its own conditional tags here, so we have to use default WordPress conditional tags:

has_term( $terms, 'product_cat', $post );

Now let’s take a look at a couple examples:

if( has_term( 4, 'product_cat' ) ) {
	// do something if current product in the loop is in product category with ID 4
if( has_term( array( 'sneakers', 'backpacks' ), 'product_cat', 50 ) {
	// do something if product with ID 50 is either in category "sneakers" or "backpacks"
} else {
	// do something else if it isn't
if( has_term( 5, 'product_tag', 971 ) ) {
	// do something if product with ID = 971 has tag with ID = 5

WooCommerce Conditional Tags

Misha Rudrastyh

Misha Rudrastyh

I develop websites since 2008, so it is total of 14 years of experience, oh my gosh. Most of all I love love love to create websites with WordPress and Gutenberg, some ideas and thoughts I share throughout my blog.

Need some developer help? Contact me

Follow me on Twitter