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 8
 * @community     https://businessbloomer.com/club/
 */
 
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 );
 
}

Advanced Plugin: WooCommerce Protected Categories

This code snippet will hide products from specific categories from the shop page, while leaving them visible in other parts of your store.

In case you wanted to hide products from specific categories more widely, I did some research and discovered that Barn2’s WooCommerce Protected Categories plugin provides an easy no-code way to do this.

It works by letting you set the visibility for each category to either password protected, or restricted to specific users or roles. You can choose whether to continue displaying products from protected categories in public parts of your site and just restrict access to the single product page; or whether to make them completely invisible. It’s a good option if you need a fine-grained way to control access to your product categories.

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce Visual Hook Guide: Archive / Shop / Cat Pages
    I’ve created a visual HTML hook guide for the WooCommerce Archive Page (which is the same page for the Shop, Category, Tag pages). This visual guide belongs to my “Visual Hook Guide Series“, that I’ve put together so that you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can […]
  • WooCommerce: Hide Prices on the Shop & Category Pages
    Interesting WooCommerce customization here. A client of mine asked me to hide/remove prices from the shop page and category pages as she wanted to drive more customers to the single product pages (i.e. increasing the click-through rate). As usual, a simple PHP snippet does the trick. I never recommend to use CSS to “hide” prices, […]
  • WooCommerce: How to Remove the “Default Sorting” Dropdown
    If the WooCommerce product sorting functionality (“Default Sorting” dropdown) is a waste of space or you don’t need that select box at all, you may want to remove it. No matter if you prefer custom code or a simple plugin – hiding the product sorting dropdown is a piece of cake. Enjoy!

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. Follow @rmelogli

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
     * @community     https://businessbloomer.com/club/
     */
      
    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? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. 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 the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *