The following code removes product categories from the WooCommerce Product Category Widget. It goes in your theme’s functions.php file. To remove only one WooCommerce product category, remove the extra ID’s and trailing commas.
UPDATE: A recent comment on this post reminded me that I wanted to update it with information on how this code works. If you take a look at the comments below, you’ll notice some idiots insisting that I “stole” this snippet from some other website. I got a kick out of the douche that tried to make me think he’s someone from Google in an attempt to scare me into removing the post. I mean, come on…are you that lame? Thanks for the laugh at your expense you scared little troll.
Anyway, I thought I’d explain how I came up with this very basic snippet that these people seem to think is so complex and detailed that I must have stolen it. Here you go:
- woocommerce_product_category_widget_args – This filter is applied inside the
wp_list_categories()
function in class-wc-widget-product-categories.php on line 226. This means that the values returned by adding the filter are passed into wp_list_categories. - wp_list_categories() – This function accepts an array of arguments. One of the keys in this array is
'exclude'
to exclude specific category IDs from the results.
This code is hooking into the woocommerce_product_category_widget_args
filter, adding a new element to $cat_args with a key of ‘exclude’ and an arrayed value of the IDs to exclude, returning $cat_args and passing it into the wp_list_categories()
function, which, subsequently, adds the ‘exclude’ key to the array and remove those category ID’s from the WooCommerce Product Category Widget.
A bit of personal advice to avoid looking like a total ass-wipe: show some restraint when it comes to accusing someone you don’t know of something you cannot prove.
Nand says
Hello,
This code is no more working in Woo Version 6.5.1
Can you please send me the updated version of this code ?
Nand
Shelley says
I have just used this in one of my sites and it works perfectly. I would double check you have the right ID assigned in the exclude section.
Matthew Wiedbrauk says
Hello and thank you for this. It is working perfectly. Not sure if this is too old now, but is there a way to include the product tag field/link/search just like the shop page, so it shows the same as the shop page? If one clicks on a product tag, it reveals all similar products with that tag, but the Category list now shows those categories that I hid using your code. Thanks.
susan green says
Hi there, is there a way of using the code to exclude sub-categories of the parent item without specifically listing all the child IDs in the array? In other words, can I exclude all the children (current and future) by excluding the parent? My situation is that each of my categories has a number of children and I only want one category (with children) showing in the widget. I want to avoid the situation where further down the line I add some more children to excluded parent categories and these show up unless I go back into functions.php to add these new subcategory IDs.
Many thanks.
Maria Terzidou says
Hi, I have the same issue. Did you find the solution?
Thanks in advance,
Maria
Morten Virik says
Hi,
The code only seems to work for the shop page, and not the category pages.
The category “STRAALE BELYSNING” is hidden here:
https://landstil.com/produkter/
But not here:
https://landstil.com/produktkategori/min-magiske-jul/
Francesco says
same here
Lyall says
Same here – anyone have a fix for this?
Jean Bon says
Bug still here. Any fix?
Verdi says
This solved my problem, thank you!
Irina says
Thanks for this snippet. However, for me, it only works on the main shop page. Once a category is selected, the excluded category shows up again in the category widget. Any thoughts?
The category in question is Subscriptions.
Thanks
Mike says
Where is class-wc-widget-product-categories.php located?
Thanks
slavik says
wp-content/plugins/woocommerce_2-12/includes/widgets/class-wc-widget-product-categories.php
Joey says
Hey Ren. I was looking all over for a plugin to remove sub-categories from the wc category widget and came across this snippet. I’m not a coder, but after a little hiccup I got it working. It works perfectly. Thanks for posting this – you really saved my bacon.
Note to others – I got a 500 Server Error when I pasted Ren’s code into my functions.php file. Being code challenged as I am, it was because I ended up using the <?php opening tag again and pissed off wordpress. Here is a screenshot of how pasted this in if anyone else gets the same thing: https://www.screencast.com/t/ygHmKg5I
Autumn says
Hi There!
I’m really confused with everyone sharing their two cents about what code works, doesn’t work, and etc. etc.
I’m looking for the exact code that will disable certain categories from showing up via the WooCommerce Product Widget Categories when “only show children of parent category” is checked.
I have tried some of the code mentioned above and it hides my categories on the shop page, but not on other individual category pages.
So I tried that include/exclude code – and it worked fine, but then back on the shop page it once again showed the categories I wanted to hide.
I need a code that does both. Hides categories on ALL woocommerce pages. Not just the shop, and not just other product categories. But ALL woocommerce pages.
Can someone please reply with the exact code? I’m so confused from everyone elses reply that I don’t know which one is the right one.
Thanks!
Josh says
Hello – Could this code be used to hide certain categories for only some user roles?
Benoit taillefer says
I would like to know if this is possible as well.
Thanks
Ren says
Sure. You should only need to wrap the
$cat_args['exclude'] = array('55','68');
line in a conditional to check the current user’s permissions. For example:function rv_exclude_wc_widget_categories( $cat_args ) {
// Hide from non-administrators
if ( ! current_user_can( 'manage_options' ) ){
$cat_args['exclude'] = array('55','68'); // Insert the product category IDs you wish to exclude
}
return $cat_args;
}
That will remove those product category IDs for any user who does NOT have the manage_options capability (anyone who is not an administrator).
You can see this link for more information on the built-in capabilities for the various roles.
https://codex.wordpress.org/Roles_and_Capabilities#Capability_vs._Role_Table
fd says
Excellent, exacty what I needed! 🙂
Thank you!
sam says
Thanks Ren. Finally managed to sort out my problem. Your masterpiece works wonders.
Ren says
Hey, Sam. Awesome! I’m glad it worked for you.
Syd says
Hi. I added the code to remove category from shop page but after updating I got this msg “Internal Server Error”. Now my shop page doesn’t work at all. I place the code in function.php but I’m not sure what went wrong.
Stuart Nelson says
Nice work! I found this super useful thanks for sharing
chris says
Very nice piece of code! But when I run it on latest WP (4.3) and WooCom (2.4.6) it’s not behaving perfect. Do you have problems too or is it just me?
On the front page it’s not hiding the excluded category in the dropdown but it is hiding it in the fully expanded menu.
On a product page it’s not hiding the excluded category at all.
Any idea what’s going on?
Sri says
Thank you for the tip and the snippet. Unfortunately it didn’t worked for me. But I could figure it out why it was not and wanted to share it here.
The ‘exclude’ list will be ignored if an ‘include’ list does also exist. So I did the following:
Ren says
Thanks for sharing, Sri!
George says
Your code seems to work only when the option “Only show children of the current category” is not selected.
In my case i have that selected in the widget and it does not work, it displays all categories.
So maybe there is a way to fix that?
George says
Hey
I have tried both codes but they dont work. Where do i place the product category ids in your code?
Thanks
Jacco says
Hi Ren, thanks for sharing!
@George same problem here! The selected option “Only show children of the current category” causes included ID’s over excluded ID’s. The exclude argument doesn’t work. Please use this code in your function:
Lisa says
Great code! Thanks to Ren & Sri.
One issue:
if($key ) {
….
}
If the first element of the array is a match, and has they key ‘0’, it’s interpreted as ‘false’ in the if statement — which is not correct.
A better test of if there a key would be:
if(isset($includes[$key])){
….
}
It would look like:
add_filter( ‘woocommerce_product_categories_widget_args’, ‘sb_exclude_wc_widget_categories’ );
function sb_exclude_wc_widget_categories( $cat_args ) {
$term = get_term_by(‘slug’, ‘uncategorized’, ‘product_cat’);
if ($term) {
$cat_args[‘exclude’] = array($term->term_id); // category to exclude
// remove category ID from include list if an include list exists
if (isset($cat_args[‘include’])) {
$includes = wp_parse_id_list($cat_args[‘include’]);
$key = array_search($term->term_id, $includes);
if(isset($includes[$key]) {
unset($includes[$key]); // unset the category from include list
}
$cat_args[‘include’] = implode(‘, ‘, $includes);
}
}
return $cat_args;
}
Bartek says
Work fine. Thank Yo very much.
Emily Murray says
Thank you so much!!!! Worked perfect!!!!! I was using another similar code but after the update it stopped working so I found this and was thrilled!
You wouldn’t happen to have a tutorial on removing a category from the main shop page would you? The code that Woo provides doesn’t work for me after the update.
Thanks again!
Ren says
I haven’t written a tutorial but have you tried using
pre_get_posts()
to modify the WooCommerce query?http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
Emily Murray says
Thanks Ren! I actually did try that. Almost all tutorials have the same code, it use to work but now it’s not:
Emily Murray says
And I also just tried the code you suggested to another in comments:
But mine are still showing on my shop page.
Ren says
I tested it out and the code is working for me. There’s no reason that I’m aware of which would cause it to randomly stop working. The code itself is entirely WordPress, it’s just being applied to a WooCommerce taxonomy. Where are you placing the code?
Ibrahim Mokdad says
this snippet worked for me; The other snippets suggesting I use the slug to id the category to be excluded did not work for me; no idea why. anyways this one did.
Ren says
Glad to hear it worked for you!
Marcus says
Hi Ren
We can’t get this to work. Tried using both variations of the codes and testing with both list and dropdown show.
This is what is in our functions.php – note that it’s a child theme. (Does that make a difference?)
Ren says
Your code is fine and everything works for me. Without seeing what’s going on, the most I could suggest is to confirm the product category ID.
Alessandro Marengo says
Hi!
Is it possible to use woocommerce_product_subcategories_args in order to use a category for a specific need but to exclude it from the loop and woo pages? I mean, even in the category list which appears in the product page.
I tried this unsuccessfully:
What do you think about?
Ren says
Try this instead
Henry says
Hi there,
This is exactly what I’m trying to do, but for some reason your code doesn’t seem to be working for me. Any opportunity I could tap you for help debugging why?
Ren says
Hi, Henry. Are you getting any errors when you use the code or does it just not do anything?
Zobboy says
Hello,
I’ve exactly the same problem (WP 4.1, WC 2.2.10).
No error message, nothing… just that the Category is not hidden.
Thank you.
Ren says
Sorry, I meant to update the Gist earlier but it slipped my mind. Since the code before only worked when the widget is shown as a list, I needed to add the proper filter for also removing the categories when the widget is shown as a dropdown. This is probably the issue you were facing (using it as a dropdown). Anyway, the code has been updated so you can swap it out or just add this line:
add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'rv_exclude_wc_widget_categories' );
J.B. says
Damn, Ren! I love seeing people like that get smacked down and you put those morons on blast. Good job! 😉
Ren says
Lol. Thanks, J.B. 🙂
Sudipa Sarkar says
Hi there Ren,
Thanks for the code and it’s clearly shown that the referred ID given by the troll (I believe all the above comments here made by one person from different IDs) has really nothing to do with your code – it’s absolutely different. Anyways, I am really thankful to you for this code.
All the best
Sudipa
Ren says
Hi Sudipa,
I don’t know how some people think they have the right to assert something they don’t know and cannot prove. After all, you see very similar and even exact snippets of code from various sites across the internet. I doubt everyone is “stealing content” from the original source.
Anyway, I’m glad the post helped you. Thanks for commenting!
Tom Ewer says
I also accuse you of content theft.
http://www.wpexplorer.com/best-woocommerce-snippets/
Please read this http://www.plagiarism.org/plagiarism-101/what-is-plagiarism
Ren says
So because information exists somewhere and then something similar (not even exact) shows up elsewhere, it’s plagiarism? If so, are you seriously going to insinuate that every bit of similar content on the internet is plagiarized from the first source it ever appeared? Please, explain HOW you think I “stole” a few lines of code because I’d love to hear it. I find that a bit strange when the code isn’t even exact. For your information, I spent a couple hours researching and testing different things to do what this bit of code did. If I had found the source you cite in your comment, I wouldn’t have had to spend so much time on it and I WOULD have cited it, just like I cite sources in other posts. So are you saying that it is not possible for anyone else to come up with a similar (or even identical) solution for a small change?
DMCA team says
The problem is you have re-published someone else’s work and passed it off as your own which is theft and is also deceptive to your readers as they may think you know what you are doing when you don’t..
In some countries this is regarded as a computer crime and you would be imprisoned.
You should do the right thing and remove all stolen content from your site and only publish your own work as that is what your readers sign up for, not other peoples work which you pass off as your own.
Don’t take this the wrong way but if you continue to steal from other sites, your site maybe deleted from your servers without warning.
Ren says
You’re not someone from Google but nice try. Aside from being completely ignorant to the comment you’re directly replying to, what you and the other people have failed to do is substantiate how I “stole” someone else’s work when I am telling you that I did not. Where is your proof, especially when the code is not even identical? Yes, it’s similar but I can write code similar to thousands of different snippets across the web that I’ve never seen. Does that mean I’m stealing their code when I’ve never even seen it? Please, can someone explain the proof they have rather than whine about how I “stole” it. I’m open to discussing this because I’d really love to know how someone thinks I stole this but if I get one more comment without any good argument, I’ll just delete the comment because I’m not going to stand for people throwing out sad accusations they can’t even back up.
DMCA team says
You didn’t write the code and you know it.
You have simply changed the name of the function and the I.D’s in the code and you know it.
This is what you have changed:
’55’,’68’ and this exclude_wc_widget_categories
Your readers aren’t stupid and they also know it.
Most if the content on your site is taken from someone else’s.
Google will not see your domain as a original source of unique content so your domain will never be an authority site.
Ren says
You’re pathetic dude. You troll my site and post comments using an email that you think is supposed to make me believe YOU are the authority and then you think your words are supposed to mean something to me. You won’t even come out and say who you are because you have to hide behind your “authoritative” name and removals@google.com email. Are you serious? How I’d love to meet people like you in person. I think your tone would be very different. Why don’t you find a life and get off my site?
Calum says
Hi Ren, thanks for the snippet, really dont understand the trolls on this post. If I had to write this from scratch its exactly how I would have done it….
Um, how many ways are there to write this code in a simple form? Is it possible that 2 people could possibly write an almost identical part of very simple code?
Answer: YES. It is possible.
If it had been a few hundred lines of code exactly the same then you may be correct in questioning someone about authenticity.
Thanks again Ren, I hope these people can get it through their thick skulls that its entirely possible that you and someone else could have written the same code completely independently of each other.
Just wow trolls, two programmers wrote the same 2 lines of code…. I bet if you asked a thousand programmers to write this without seeing it you would be accusing them all of copying each other.
Some goddam stupids on the internet….
Ren says
Hi, Calum. Well said! I couldn’t agree with you more. Thanks for commenting and I appreciate you supporting me on this. Also, I’m glad the post helped you. Cheers! 🙂
Remi says
This is not your code.
Nor have you acknowledged the original source of the work.
Ren says
And you show up on someone’s blog just accusing something like this?