WooCommerce: Exclude 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]); in fact, there is no parameter that allows you to “exclude” a given product category such as “uncategorized” or whatever category you wish.

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 “Edit” link and note down the ID which is contained within the URL:

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
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */
 
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', 'fields' => 'ids' ) );
   echo 'Category IDs: ' . implode( ', ', $categories );
}

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
// ## Include all but "8":
 
[product_categories ids="15, 34, 20, 18, 37, 10"]

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: Display All Products Purchased by User
    When a WooCommerce customer is logged in, you might want to show them the list of previously purchased products (maybe in a custom “My Account” tab). This is helpful when customers tend to buy the same products over and over again, and therefore you can help them “order again” without having them to search the […]
  • 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!
  • WooCommerce: Show “Sold Out” @ Shop Page
    Here’s another simple snippet that can easily help user experience and make sure a “sold out” badge shows on each out of stock product in the category & shop pages. Not all themes allow this so you can use the snippet below to make it happen!
  • WooCommerce: Display Out of Stock Products (Shortcode)
    A client of mine wanted to show out of stock products on a separate page – so I coded a simple shortcode for you all! You can use this shortcode for different goals. For example, you might want to display what products you’ve sold to enhance customer trust / social proof. So let’s see (1) […]
  • WooCommerce: Display % Discount @ Shop Page
    Default WooCommerce shows a “Sale” badge if the item is on sale – but what about showing the exact sale percentage instead? I implemented this for one of my freelance clients so here you go with the easy-peasy solution. 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

56 thoughts on “WooCommerce: Exclude Category from ‘product_categories’ Shortcode

  1. Hello Rudolfo, thank you so much!!! After all these years I was still messing with this category code, via shortcuts. It just did not work on my site, and I tried many codes. But then I found yours and – it worked! I enter shortcodes in Gutenberg-blocks. And if I enter this [product_categories ids="001, 002"] it shows everything just like I wanted! Phantastic. The secret for me was the “ids=” thing. I tried it with names and numbers before, but not via “ids” but via “categories” and that in several spellings and combinations and all that. Anyway thank you so much! Your code lasted, we have 2021!! Maybe it can be of help for someone else.

  2. Hi!

    I donยดt know if i did it correctly but Iยดm getting this error:

    syntax error, unexpected 'ids' (T_STRING), expecting ' ] '

    Thanks!

    1. Not 100% sure, but actually you don’t need the snippet at all, it’s only to find out the IDs. Just work with the shortcode

  3. 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.

  4. 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?

  5. 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/

  6. 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 ๐Ÿ™‚

  7. 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 ๐Ÿ™‚

  8. 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 ๐Ÿ™‚

  9. 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

  10. 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

  11. 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!

  12. 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 ๐Ÿ™‚

  13. Hi Rodolfo,

    Thanks for the help

  14. 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 ๐Ÿ™‚

  15. 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

  16. 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?

  17. 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?

  18. 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 ๐Ÿ™‚

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

  20. 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 ๐Ÿ™‚

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

    1. Awesome Frank! Thank you ๐Ÿ™‚

  22. 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;
      }

  23. 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!

  24. 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!

  25. 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 ๐Ÿ™‚

  26. 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? 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 *