I want to display a different description for a post if it belongs to a specific product category in a filter.

I've used the code below code to add the code but it is displaying for all products:

function herara_filter_short_description( $desc ){
    global $product;
    if ( is_single( $product->id ) ) {
        $desc = '<span class="sku_wrapper">SKU: <span class="sku">'. $product->get_sku() .'</span></span></BR><span class="material_wrapper">MATERIAL: <span class="material">'. $product->get_sku() .'</span></span>';
    return $desc;
add_filter( 'woocommerce_short_description', 'herara_filter_short_description' );
  • so you're asking if a post ( of type product ) has a particular term in a taxonomy? ( where the taxonomy is the woocommerce product category taxonomy? ). How to test if a post has a term in a taxonomy is a much better question that achieves the same thing and is 100x easier to find an answer for.
    – Tom J Nowell
    Oct 5, 2020 at 11:02
  • I've cleaned up your code and fixed an unrelated bug ( is is_single was false then null would be returned, overwriting any other filters, so I changed $new_desc to $desc. It's also not necessary to declare the $product global and pass its ID, the current post is already set to that product so it already knows which post to check
    – Tom J Nowell
    Oct 5, 2020 at 11:07
  • no not that, am talking about a product (Woocommerce), if the product is in the category XX a specific code should display in the short description. Oct 5, 2020 at 11:10
  • Yes, products are posts of type product, much like pages are posts of type page, products are just another custom post type. Much like categories and tags are taxonomies, so are product categories, under the hood they're all the same. ( note that if they were not, then this Q would be off topic, 3rd party plugin dev support is offtopic here so your question would have been closed )
    – Tom J Nowell
    Oct 5, 2020 at 11:12
  • even the WooCommerce official docs say so docs.woocommerce.com/document/installed-taxonomies-post-types, product categories are just a custom taxonomy with the internal name product_cat
    – Tom J Nowell
    Oct 5, 2020 at 11:12

1 Answer 1


You can test if a post has a particular term assigned to it with the has_term function.


if ( has_term( 'term-slug', 'taxonomy-slug' ) ) {
    // do stuff

With this you can test if your "post" has a particular "term" ( aka if your "product" has a particular "product_cat" )

  • sorry not able to understand anything - hope my request is clear. for example if any product in the category men's leather shoes i want to display in the short description the SKU & the Material: Leather, and so on, that is it Oct 5, 2020 at 11:24
  • I'm not sure which part you're having difficulty with, this is the solution to your problem. If I take your comment as an example: "for example if any product post has the term mens leather shoes in the product_cat taxonomy, aka if ( has_term( 'mens shoes', 'product_cat' ) ) { echo "the SKU & the material leather and so on"; }.
    – Tom J Nowell
    Oct 5, 2020 at 11:31
  • The fundamental missing information was that Product Categories is just a custom taxonomy, I recommend reading up on custom taxonomies and custom post types. Everything from shop orders to variations is either a custom post type or a custom taxonomy. Understanding this will make life much easier and allow you to work a lot faster
    – Tom J Nowell
    Oct 5, 2020 at 11:34
  • Can you share the full code that i need to just put it in the function file Oct 5, 2020 at 11:41
  • My last comment is almost identical to what the end result would be, if you're looking for something you can blindly copy without even basic understanding then no I can't, even if I wanted to as you didn't share the slug of the product category you're trying to test for. But since I've already given you the code in the above comment, you could just replace mens shoes with the slug, you don't need my help to do a basic string replacement
    – Tom J Nowell
    Oct 5, 2020 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.