WooCommerce: Hide a Category from [product_categories] Shortcode

Sometimes solutions are very simple, and you don’t need rocket science to fix your issue! A client of mine needed to hide a category from the Product Categories Shortcode ([product_categories]). A rare issue, but once again a quick fix! Here’s how you do it.

1. Find WooCommerce Category IDs

First of all you need a list of all the WooCommerce category IDs in your store. To do so you can proceed in two ways.

The first is purely manual. This is good if you have no more than 20 categories. Go to the Product Categories page, hover on each category and note down the ID as shown in the image.

Find WooCommerce category IDs

The second method is a little smarter ๐Ÿ™‚ You can use PHP in the admin to print a list of category IDs, so you don’t need to worry about doing that manually.

You can use this snippet to print a list of all Category IDs:

/**
 * @snippet       Print List of Category IDs @ Product Categories Admin
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_action( 'product_cat_pre_add_form', 'bbloomer_list_all_product_cat_ids', 5 );
 
function bbloomer_list_all_product_cat_ids() {
 
$ids = '';
 
$categories = get_categories( array(               
   'taxonomy' => 'product_cat' ) );
  
foreach( $categories as $category ) {
    $ids .= $category->term_id . ', ';
} 
 
echo 'Category IDs: ' . $ids;
}

Here’s the result:

Get a list of Product Category IDs

2. Exclude the Category IDs from the “product_categories” Shortcode

Now that you have all IDs and the ID of the category you want to hide, simply exclude that from the list of ids inside the shortcode!

// -----------------------
// #1 Show all categories
 
[product_categories]
 
// -----------------------
// #2 Show all categories but ID = 8
// ## All category ids: 8, 15, 34, 20, 18, 37, 10
// ## See, this last version is missing the "8"!
 
[product_categories ids="15, 34, 20, 18, 37, 10"]

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). 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+.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce Customization?

Check out these free video tutorials. You can start learning how to customize WooCommerce without unnecessary plugins. Watch me code and learn by example!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a 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.

52 thoughts on “WooCommerce: Hide a Category from [product_categories] Shortcode

  1. Can you also recommend a plugin that supports hiding product categories? I’m not really familiar with coding and don’t want to make changes to important files like functions.php files.

  2. Rodolfo I dont see any of the updates youve mentioned youve made to the original post. Are they elsewhere?

    1. Hello Wendy! Sorry, I’m not sure about that – could you be more specific please?

  3. HI Rodolfo,
    Rather than writing all the available to use ID’s and then excluding 1 ID, would it not be better to just exclude the 1 ID from all available ID’s? Seems your doing this the hard way… or am I mistaken?

    For example, our company adds new categories every week. With your method, we would require going back into your php snippet and adding the new category ID to the list of available ID’s every week! Surely there is a better way around this by just excluding the 1 ID and never have to edit the snippet again.

    1. Hello Dayle, thanks for your comment! The [[product_categories]] shortcode is not that flexible I’m afraid, as it gives no “exclude” option. You’ll need instead to use just [[product_categories]] and then modify the output via PHP, similar to this: https://businessbloomer.com/woocommerce-exclude-category-from-products-shortcode/

  4. Hi Rudolfo,

    I found your website and wondering if it is still valuable 2 years later?
    And I have some questions about the code (see in between with large arrow):

    // -----------------------
    // NOTE: in order to not execute the shortcodes, I had to add spaces inside the shortcodes 
    // -----------------------
     ------------------------------> I don't understand this remark. Does it mean I have to undo the spaces?
    // -----------------------
    // #1 Show all categories
     
    [ product_categories ]
     -------------------------------> If I put this shortcode in the CSS isn't that in contradiction of the second one? As I don't want to show all categories
    
    // -----------------------
    // #2 Show all categories but ID = 8
    // ## All category ids: 8, 15, 34, 20, 18, 37, 10
    // ## See, this last version is missing the "8"!
     
    [ product_categories ids="15, 34, 20, 18, 37, 10" ]
    ---------------------> I put this in the function.php of the childtheme but it didn't work
    
    

    Frank added this as a question:
    .woocommerce ul.products li:nth-of-type(16) {
    display: none;
    }
    You gave as answer to use the ID of the page where you want to hide the category and use li:last-of-type, but is it possible to exclude the category by it’s ID in a Frank posted like formula?

    Thanks,
    Luc

    1. Hey Luc, thanks so much for your comment! Shortcodes go into the WordPress page/post content, not inside CSS/PHP. Try there and you will see how this works ๐Ÿ™‚

  5. Would it not be cleaner to do this with a custom function? That way it’s not necessary to move the category to the end, etc.

    function custom_pre_get_posts_query( $q ) {
        $tax_query = (array) $q->get( 'tax_query' );
        $tax_query[] = array(
               'taxonomy' => 'product_cat',
               'field' => 'slug',
               'terms' => array( 'CATEGORY-TO-HIDE' ), // Don't display products in the this category on the shop page.
               'operator' => 'NOT IN'
        );
        $q->set( 'tax_query', $tax_query );
    }
    add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' ); 

    Or does this not achieve what you’re solution achieves? Perhaps I’ve overlooked something…

    1. Totally right Jonathan – so I spent the last 30 minutes completely revising and rewriting this article. Check out the new solution now ๐Ÿ™‚

    2. I found this which will make it page specific.

      add_action( 'woocommerce_product_query', 'prefix_custom_pre_get_posts_query' );
      /**
       * Hide Product Cateories from targetted pages in WooCommerce
       * @link https://wpbeaches.com/hide-woocommerce-product-categories-shop-page/
       */
      function prefix_custom_pre_get_posts_query( $q ) {
      	
      	if( is_shop() || is_page('awards') ) { // set conditions here
      	    $tax_query = (array) $q->get( 'tax_query' );
      	
      	    $tax_query[] = array(
      	           'taxonomy' => 'product_cat',
      	           'field'    => 'slug',
      	           'terms'    => array( 'cat1', 'cat2' ), // set product categories here
      	           'operator' => 'NOT IN'
      	    );
      
      	    $q->set( 'tax_query', $tax_query );
      	}
      }
      
      1. Nice ๐Ÿ™‚

  6. Thanks for the post.
    I did get it to work but it took products off other pages.
    I would like to remove just off the shop page
    How would I change it to work for just that page?
    Thanks

    1. Hey Wyatt, 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 ๐Ÿ™‚

  7. Hi there
    Thanks for a great post!

    I have one small issue;
    .woocommerce ul.products li:last-of-type {
    display: none;
    }
    Works perfectly on the online shop page to hide the category (Coming Soon), however this also hides the last product in each of the remaining categories when you navigate to these pages.

    When I use
    .post-4 .woocommerce ul.products li:last-of-type {
    display: none;
    }
    OR
    .online_shop .woocommerce ul.products li:last-of-type {
    display: none;
    }

    Where 4 is the post id for the online shop page, and the page name is as per the url, the removal of the category on the online shop page no longer works. I have also tried to substitute nth-of-type(7) which also works perfectly, until i try to specify the page.

    Please can you help?

    1. Hey Kim! Yes, that call might be wrong: .post-4 .woocommerce

      This is 100% custom to your own theme and combination of plugins, so I’m afraid I can’t offer support here via the blog ๐Ÿ™‚

    2. Hi there

      No problem at all – I re-authored the page. All sorted.

      Thank you for the reply

  8. Hi, i need to hide some categories depending on the year season.
    How do you suggest to do that?

    Thanks in advance!!

    1. Hola Matias, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here on the blog. Hopefully I will have time to create a snippet soon ๐Ÿ™‚ Thanks a lot for your understanding! ~R

  9. Hi Rodolfo,

    I am having some trouble with my Store page and I might me making this harder than it needs to be.

    I want to use Show categories and products view for woocommerce, I have 4 categories I want to hide 3 of them but show the products

    the last category has a lot of items in it so I wanted to hide those products on the store page but leave the category.

    The reason why I want to do it this way is because I am using the advanced notifications addon ( which is not very advanced..) this addon lets me set a certain category to a certain email which then sends the invoice out. All 4 categories have different email recipients, Normally i would use the categories but my client has a lot of older users and are confused by the categories.. they click on the category and dont click on the product so they think it isn’t working.

    Is there an easier way to do this? Is this possible?

    1. Hey Kurt, thanks for your comment! I’m sure you can target specific elements of the loop via CSS (despite it’s not the ideal way, but in this case it would be more cost-effective) – or if you have no unique CSS class you can target, you can always add the name or the ID of the category as a CSS class to those elements via PHP, and then target them via CSS. Hope this helps!

  10. Thank you for your great tutorial. I understand this is an old post, but I do have a question.

    I wanted to hide 4 categories and that part worked. But when I searched for an item in a category that I have hid, the search came up with some results from what I wanted to hide. Is there a way to avoid that? I want to make sure the categories and their products are hidden from any customers or potential searches.

    Thank you in advance.

    1. Hey Mohammed thanks for your comment! You can simply set each single product to “hidden” (from the edit product page, top right). In this way they will be hidden everywhere! Let me know ๐Ÿ™‚

  11. Hi Rodolfo,

    Thanks for the help

  12. Hello

    this one is to hide all item in the same categorie
    in this example, the category name is””test*, so you have to replace “test” by the name of your categorie

    /*hides all items on specific category */
    .woocommerce .product_cat-test {
    display: none;
    }

    have fun

    1. Thank you Yann for your valuable feedback! Keep it coming ๐Ÿ™‚

  13. Question about WooCommerce. My client sent me this email and I am trying to see what I can do. MY CLIENT “So I was just going through my quarterly taxes and figured out what was wrong with the sales tax. You remember when I mentioned it was charging some MA sales sales tax but not all of them? I figured it out! It only charges MA tax when the billing and shipping are in MA….if the billing is in MA and it’s shipping to another state it doesn’t charge sales tax and should.”

    Basically is they a way to change this part? “if the billing is in MA and it’s shipping to another state it doesn’t charge sales tax and should?”

    1. Sorry for the delay ๐Ÿ™‚ Yes, this is possible! Go to WooCommerce/Settings/Tax and in the “Calculate Tax Based On:” choose “Shop Base Address”. Hope this helps! R

  14. Hello! Thanks for posting this, I am trying this bur can’t make it to work on my website ๐Ÿ™

    1. Thanks for your feedback Gaby! Can you post your custom snippet here, I’ll take a look?

  15. Hi Rodolfo,
    I’m trying but it doesn’t work at all. Tried also to past the code in the custom css plugin but i see no change. i’m trying to delete a category from a page with this simple shortcode [all_products columns=”3″]. Any advice?

    1. ps it deletes only the last product of the list and not the entire category in the last position…

      1. Alex, what code are you using?

  16. Seeing that drag option doesn’t work I am using this short code and placing it into columns of 5 that way I can drag from there. [product id="15145" sku=""]

    Again thank you so much!!!

    1. Awesome, no problem ๐Ÿ™‚

  17. Tried that and it didn’t work. prob a plugin affecting that feature.

  18. One last question if you view the gift card page you will notice the 250 card is first.

    I am trying to arrange this with a drag option or some idea so we can sort that out.

    1. Frank, if you go to your WordPress back-end and go to Products, you can click on “Sort Products” and drag/drop them in the order you like ๐Ÿ™‚

  19. Great that did the trick, Thank you so much, I bookmarked your page for future help. ๐Ÿ™‚

    1. Awesome Frank! Thank you ๐Ÿ™‚

  20. I added the css style

    .woocommerce ul.products li:last-of-type {
    display: none;
    }

    but noticed one of the products in the gift card is gone, is there a way to not show just that category by using the ID number that way all five products show on the gift card page and not the category on the home page?

    What does this css style do, I see the number 16 is that the product ID?

    /* If the hidden-to-be category is in position #16… */

    .woocommerce ul.products li:nth-of-type(16) {
    display: none;
    }

    1. Frank, thanks for your message! You’re right, this CSS code also acts on the products loop – so make sure to prepend the ID of the page where you want to hide the category, e.g.:

      /* hides on homepage*/
      .home .woocommerce ul.products li:last-of-type {
      display: none;
      }

      /*hides on specific page or post id*/
      .post-987 .woocommerce ul.products li:last-of-type {
      display: none;
      }

  21. Your code worked perfectly, EXCEPT I only want to hide the first 2 categories on our home page (Which is a product shopping page). The two categories are for customization options within the products, but we don’t want them visible. I hope this makes sense, and unfortunately we can not make the site public for you to look at.

    What we are encountering is across the board the categories are hidden, and not just the shop page. So when you click “Mens” the first two categories are now hidden.

    Please help any way you can, thank you!

    Ashley

    1. Ashley, thanks so much for your feedback. If you only want to hide the categories on the homepage, simply use the CSS class “.home” before that code I gave you, to make sure the code is added to the homepage only. Let me know if this works!

  22. Where exactly do I put this snippet of code? I’ve tried styles.css and a few other pages, but it never hides the category from lists :[

    1. Anthony, yes, definitely in the style.css file – or use a plugin called Simple Custom CSS where you can paste the code. If this does not work for you, simply tell me your web address and the category you’re trying to hide and I’ll send you the CSS snippet!

  23. Why would you not just use :last-of-type or :last-child. that way if you add new products, its a little more dynamic.

    1. Thank you Rudi, you’re right! I just updated the post ๐Ÿ™‚

  24. Thank you so much for this!!!!

    I have 2 categories I did not want showing up on a certain page and all the codes I found were to remove from the shop page, the problem is my “shop” page is not the one that displays all my categories so that general code to exclude from shop page did not apply. I then tried the “exclude” rule in the shortcode and that didn’t worked bur after hours of trial, error, and searching I found your tutorial and it worked perfectly.

    I needed two categories removed so I just used the same code twice.

    Thanks again you have no idea how helpful this was!!!!

    1. Emily, it’s great to hear that! Thank you so much ๐Ÿ™‚

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

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

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