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:
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:
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"]
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.
Excellent!
Hi!
I donยดt know if i did it correctly but Iยดm getting this error:
Thanks!
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
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.
Hi Suzie! #4 here will do that: https://businessbloomer.com/how-to-build-a-successful-woocommerce-wholesale-store/
Rodolfo I dont see any of the updates youve mentioned youve made to the original post. Are they elsewhere?
Hello Wendy! Sorry, I’m not sure about that – could you be more specific please?
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.
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/
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):
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
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 ๐
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.
Or does this not achieve what you’re solution achieves? Perhaps I’ve overlooked something…
Totally right Jonathan – so I spent the last 30 minutes completely revising and rewriting this article. Check out the new solution now ๐
I found this which will make it page specific.
Nice ๐
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
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 ๐
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?
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 ๐
Hi there
No problem at all – I re-authored the page. All sorted.
Thank you for the reply
Cool Kim ๐
Hi, i need to hide some categories depending on the year season.
How do you suggest to do that?
Thanks in advance!!
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
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?
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!
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.
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 ๐
Hi Rodolfo,
Thanks for the help
You’re welcome Tony!
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
Thank you Yann for your valuable feedback! Keep it coming ๐
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?”
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
Hello! Thanks for posting this, I am trying this bur can’t make it to work on my website ๐
Thanks for your feedback Gaby! Can you post your custom snippet here, I’ll take a look?
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?
ps it deletes only the last product of the list and not the entire category in the last position…
Alex, what code are you using?
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!!!
Awesome, no problem ๐
Tried that and it didn’t work. prob a plugin affecting that feature.
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.
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 ๐
Great that did the trick, Thank you so much, I bookmarked your page for future help. ๐
Awesome Frank! Thank you ๐
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;
}
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;
}
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
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!
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 :[
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!
Why would you not just use :last-of-type or :last-child. that way if you add new products, its a little more dynamic.
Thank you Rudi, you’re right! I just updated the post ๐
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!!!!
Emily, it’s great to hear that! Thank you so much ๐