WooCommerce: Hide Products From Specific Category @ Shop

A very handy snippet. Sometimes, you only want to show certain categories on the shop page, and have those products ONLY show under the category archive instead.

WooCommerce: hide products belonging to a specific category

PHP Snippet: Hide Products From Specific Category @ Shop – WooCommerce

/**
 * @snippet       Hide Products From Specific Category @ Shop
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.6.3
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_action( 'woocommerce_product_query', 'bbloomer_hide_products_category_shop' );
  
function bbloomer_hide_products_category_shop( $q ) {
 
    $tax_query = (array) $q->get( 'tax_query' );
 
    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'chairs' ), // Category slug here
           'operator' => 'NOT IN'
    );
 
 
    $q->set( 'tax_query', $tax_query );
 
}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

53 thoughts on “WooCommerce: Hide Products From Specific Category @ Shop

  1. Thank you so much, I was actually stuck with this since 2 days. Finally the snippet you shared above worked for me. Thank you once again.

  2. Hi, thanks for the tip but im trying to exclude all products from specific custom taxonomy, i try this but doesnt work.

    add_action( 'woocommerce_product_query', 'bbloomer_hide_products_category_shop' );
       
    function bbloomer_hide_products_category_shop( $q ) {
      
        $tax_query = (array) $q->get( 'tax_query' );
      
        $tax_query[] = array(
               'taxonomy' => 'accesorios',
               'field' => 'slug',
               'operator' => 'NOT IN'
        );
      
      
        $q->set( 'tax_query', $tax_query );
      
    }

    .

    Thanks

    1. Hi Gaston, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Thank you for the snippet. It works well.

  4. Works great! WP 5.4.2 and WC 4.2.2.
    But, what would be the syntax if I wanted to remove multiple categories? Say chair AND tables.

    1. Thanks Andreas. Try with array( ‘chairs’, ‘tables )

  5. Hi, thank you for this snippet (Hide Products From Specific Category @ Shop โ€“ WooCommerce). It works but when i click on the category, now it doesn’t display the product. It shows empty.

    1. Maybe because they’re hidden?

  6. Hi Rodolfo,

    I find this article very helpful, however I need a little bit different result and hope that you can help:

    I’d like to hide the products on specific category pages (not load at all and don’t have links to, not just hide using CSS). For example, I do have parent categories where I would like to show only specific custom added content (images with links to child-categories) and no products. I would like to show products only on the child categories.
    I don’t want to do this on all parent category pages, but only on specific. Also, the site is in cyrillic so I guess that page IDs have to be used as I can’t add cyrillic in php.

    Thanks for your time and support!

    1. Stephan, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  7. How about i want to hide it the specific category in my frontpage? how can i achieve that?

    1. Hello Penny, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  8. This still work, but how about the single product under the hidden category? possible also hide it?

    1. Adrian, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  9. Still works great Rodolpho ! Thanks a lot ๐Ÿ™‚

    1. Awesome!

  10. Fantastic Rodolfo!
    Works great! Maybe a strange question but is there a way to make it hide all products?

    Instead of writing several categories – ‘chairs’, ‘tables’, ‘sofas’ etc can we have something like ‘all_categories’?

    Best regards.

  11. Hi,

    i need to hide the products of a certain category which your code works fine but i need them to be hidden for the users that have not bought anything.

    To be more exact i need to combine your code with this (also your code)

    /**
     * @snippet       WooCommerce Check if User Has Purchased Product
     * @how-to        Get CustomizeWoo.com FREE
     * @author        Rodolfo Melogli
     * @compatible    WooCommerce 3.6.3
     * @donate $9     https://businessbloomer.com/bloomer-armada/
     */
      
    add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_user_logged_in_product_already_bought', 30 );
      
    function bbloomer_user_logged_in_product_already_bought() {
    global $product;
    if ( ! is_user_logged_in() ) return;
    $current_user = wp_get_current_user();
    if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() ) ) echo '<div class="user-bought">&hearts; Hey ' . $current_user->first_name . ', you\'ve purchased this in the past. Buy again?</div>';
    }
    1. Hello John, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  12. Thank a lot, Rodolfo! I’ve tried some other snippets (WP, Stack…) but yours is the only one that worked for me (WP v5.2.3).

    1. Great!

  13. This is not working for me :
    add_action( ‘woocommerce_product_query’, ‘bbloomer_hide_products_category_shop’ );

    function bbloomer_hide_products_category_shop( $q ) {

    $tax_query = (array) $q->get( ‘tax_query’ );

    $tax_query[] = array(
    ‘taxonomy’ => ‘product_cat’,
    ‘field’ => ‘slug’,
    ‘terms’ => array( ‘retail’,’retailer’ ), // Category slug here
    ‘operator’ => ‘NOT IN’
    );

    $q->set( ‘tax_query’, $tax_query );

    }

    1. Pity, works for me. Sure about the category slugs?

  14. hello,
    can you publish this article ?
    PHP snippet: Hide Products From Specific Category @ RELATED PRODUCTS โ€“ WooCommerce

    I helped you, now you already have the title ๐Ÿ™‚

    Thank you for your great job.

    1. Maybe one day ๐Ÿ™‚

  15. Hello,

    does this still work? Do I only have to replace “chairs” with the category name that I want to hide?

    Thank you.

    1. Hey Nick, yes, still works ๐Ÿ™‚

  16. Tried it and it does remove the products from one category from the shop page, BUT, it also removes the same products from the category page they belong to… Any way to remove them from the shop page only and leave them visible in the category they belong to?

    1. Hello Michael, thanks for your comment ๐Ÿ™‚ I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know ๐Ÿ™‚

    2. Hello, I have the same problem, I would like the products to only be hidden from the shop page, is it possible to know how to do it ?

      Thank you.

  17. Works like a charm! thanks mate

  18. I want to hide one product category from shop page for example :- there are 4 categories teaching, dancing, writing, singing, so here how do i hide teaching category from the shop page?

    1. Hey Deepa, did you follow the tutorial?

  19. This works, but it also hides the products on that specific category page. For example I want to hide my free products on main shop but show them on the free products page. Where should I place the code in this case?

    1. Iulian, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  20. Hi Rodolfo,
    Would it be possible to hide specific products from a category?
    So for example if a category has 5 products in it can I hide 2 of them using their products ID’s perhaps?
    I hope you don’t mind me asking ๐Ÿ™‚

    Kinda regards,
    JP

    1. Hello again Rodolfo,
      I just realised this probably doesn’t make much sense as it is… simply don’t add them to the category would be the obvious answer haha!

      So I have a plugin that shows all variable product variations on the front end but I only want to show some of the variations in specific categories (all variations show in ‘Batman’ category but only some variations show in ‘Batman Variants’ category for example).
      As I can’t select categories for specific variations I’m hoping I can instead hide the ones I don’t want to show.

      I hope this makes sense haha!
      Thanks again for you help ๐Ÿ™‚

      Kind regards,
      JP

      1. Hey JP, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  21. Grazie, era quello che cercavo e approfitto per segnalare che lo snippet funziona anche su woocommerce versione 3.2.6.

    1. Excellent, thank you Rodolfo! (I found someone with the same name too!)

  22. I would like to remove sold out items from the shop loop into an archive. Will this snippet work?

    1. Hey Lana, thanks for your comment! You can hide out of stock items from the dashboard: WooCommerce > Settings > Products > Inventory > Hide out of stock items from the catalog

  23. Hi Rodolfo,
    Thanks for the snippet!!

    I was curious, what if it is a page(.page-template)? Are we able to exclude categories from these loops as well?

    Thank you!
    Luke

  24. Nope. I’m on WooCommerce 3.1.1 using the X theme and none of the fixes I’m finding online seem to work. You can stand on your head, you can not hide any categories at all.

    1. Danny, thanks for your comment! If you switch theme and re-apply the snippet to the new theme, does the snippet work?

  25. The code works but it always highlight
    No products were found matching your selection.
    in the shop area

    1. Hey Dol, thanks so much for your message! So, I just tested this on my shop page and it works perfectly. I’ve got a couple of questions for you:

      1) Are you trying to hide your only product category? If yes, WooCommerce won’t find any products ๐Ÿ™‚
      2) Are you trying to make a search? I remember this might give problems sometimes.

      Let me know!

  26. Hi great snippet! Thx for the work. How can I manage it with the above code that on search results the categories are displayed again. right now they are also removed from the search.
    thx

    1. Hey George thanks for your feedback! You could use this conditional tag inside the function to disable it from working on the search pages:

      if ( !is_search() )

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Your email address will not be published.