Check if Product Belongs to a Product Category or Tag

In this tutorial I will show how to check in the code if a current product in the loop or a product with a certain ID belongs to specific product categories or tags.

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 love WordPress, WooCommerce and Gutenberg so much. 10 yrs of experience.

Need some custom developer help? Let me know

Follow Misha

Need some help with WooCommerce?

If you need some professional developer help, I will be happy to assist you.

Contact me Who I am?

Leave a comment

php js HTML CSS Code

I will only use your personal information to contact you. Privacy Policy