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?

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

has_term( $terms = '', $taxonomy = '', $post = null )
$terms
Pass product category names / slugs or IDs here, you can pass multiple values as an array.
$taxonomy
The second parameter is a taxonomy name, but now we are going to use it for product categories, so set product_cat here. If you want to use it for product tags, just replace it with product_tag.
$post
Product ID or WP_Post object here. If not provided, defaults to the current product in the loop

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
}
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X