By default, the WordPress wp_list_categories function will…get a list of categories. Surprising, I know. It will also even exclude empty categories if you want, meaning if a category has no items belonging to it, it will not display it.

It’s awesome, I really dig this function. The Woocommerce Product Categories Widget uses it to display a list of categories for your Woocommerce products. It will even hide categories that do not have any products in them, much like the the default behavior for post categories, etc.

Where it doesn’t always work though, is when the product is published, but isn’t visible, due to stock or backorder status…so here’s a quick function that will look at the categories in the product list, find the products in the category, and if none of the products are visible, it will not display the category in the widget:

Posted by Chris Klosowski

Chris Klosowski is the Director of Technology at Sandhills Development, LLC, creators of fine WordPress plugins, and ecommerce solutions.

55 Comments

  1. Nachum Eliyahu June 21, 2016 at 2:39 pm

    Legit! Thank you!!!!

    Reply

  2. thanks so much chris , it is exactly what i was looking for days now !!!
    but can you tell me how to install this code please?
    where to past it?

    thanks a lot

    Reply

    1. Chris Klosowski March 25, 2017 at 8:42 am

      @manoz,

      No problem! I’ve updated the code snippet to be a standalone plugin. You can simply click the ‘Get Plugin File’ button on the page now, and you’ll get a .zip file. You can install it like any other plugin by going to `Plugins > Add New` and clicking the `Upload Plugin` button at the top.

      From there you can just pick the .zip file you downloaded from me, install, and activate it and that should do the trick!

      Reply

  3. Nice…. that solved a annoying problem for me, (empty categories) thank you very much for this plugin

    Reply

  4. Thanks so much Chris!

    I ctivated and my listings on the menu and it worked, but got totally crazy: showing all the categories without making a difference between parent-categories. I had worked an a display menu, where you could click on MEN, then on SHIRTS, for example.

    Any idea/help?

    Thanks!

    Reply

    1. Chris Klosowski July 19, 2017 at 9:30 am

      Can you provide me a link to show me an example?

      Reply

  5. Hi Chrisk,
    Thank for the plugin I insatlled it on my site http://www.optonetonline.net I am using woocommerce 4.7.5 and it is not working, so even after I installed it and activate it and even if i have hide out of stock products in woocommerce settings still shows categories with 0 products. like the link bellow
    http://www.optonetonline.net/product-category/sunglasses/emilio-pucci-sunglasses/
    Here for example the category EP003 and also the EP004 they have 0 proucts but still I can see the categories.
    Thanks for your reply,
    Stavros.

    Reply

    1. Chris Klosowski July 19, 2017 at 9:29 am

      @Stavros

      I’m not sure I follow, there isn’t a WooCommerce 4.7.5. Their latest version is 3.1.1.

      Reply

  6. Love your plugin. Does exactly what I need, when I have hidden items in a category.
    I do have an issue though.
    When I go into a child category, all of the child categories are shown, even from other parent categories.
    I just want the parent category I clicked expanded, the rest should stay collapsed.

    Reply

  7. I use the SHOPP shopping cart for WordPress. I need to be able to hide certain products from search engines. If I cannot do that I need to be able to password the product pages. Anyone got a way to do this?

    Reply

    1. Chris Klosowski November 8, 2017 at 2:27 am

      Hey Dr. Martin,

      If you’re using Yoast SEO you can do it within their metabox when editing a product:
      https://kb.yoast.com/kb/how-do-i-noindex-urls/#single

      Reply

  8. Thanks so much! That issue was driving me crazy. I’m so glad I came across your page.

    Reply

  9. Thanks a lot! This really helped me and it’s still working great! I just wanted to help people that want to hide categories that have no products in stock.

    In the plugin code just change this: $visible_product = $product->is_visible();
    To this: $visible_product = $product->is_in_stock( );

    Reply

  10. I have the latest version of WordPress and it does not work

    Reply

  11. Jacques Van dongen January 7, 2019 at 8:42 am

    Works great, Thnx.

    Reply

  12. doesn’t work on menus. It would be great if you could make a plugin to hide categories with empty product stock. So when there is no stock of some product the category dissappear and when there is stock it appears again automatically.

    Reply

      1. Just found this post. Thank you Chris for sharing this solution!

        Like Alejandro, I am trying to apply this solution to Menus in order to hide product categories which have no product in stock or have no visible products in the category.

        Fredrik – the solution linked is not working to remove categories with items assigned but out of stock. The ‘count’ column in ‘term_taxonomy’ will not have a value of zero if products are still assigned to that category but not visible because the products lack stock.

        Chris – Is there a way to modify this solution to remove categories from menus which lack visible products?

        Thanks again!

        Reply

  13. It does the job of hiding empty categories, thanks! You would think that Woocommerce would do that by default though but that’s not the point. The problem I have on my Retailer theme is that it messes up the categories list by opening all the sub-menus and listing subsequently all the categories and sub-categories which is not the way the theme works, really too bad, that was so close…

    Reply

    1. I have a similar issue, it’s opening all the subcategories, even if the widget settings are set to open only the current category subcategories, any ideas?

      Reply

  14. I was very happy to seek out this internet-site.I wanted to thanks for your time for this wonderful learn!! I definitely having fun with every little little bit of it and I’ve you bookmarked to take a look at new stuff you blog post. HottieTap

    Reply

  15. Hi there, its not working when runnign woocommerce Version 4.8.0. @Chris Klosowski any way you could update this plugin pls?

    Reply

  16. Mostly works for me, thanks! Added the filter for dropdown version of the product category widget: https://gist.github.com/movielady/a084e21431fcd17591cbb398cde2d0c1

    Reply

  17. Thanks, Chris.

    Reply

  18. Hi Chris,

    Thanks for your plugin! I have an affiliate website. I am using Max Mega Menu. I was wondering if you know how to hide categories with products in status: draft and catalog visibility: hidden from my menu.

    My menu will keep showing these categories because they are not really empty.

    I hope you can help me.

    Reply

  19. Awesome write-up. I’m a regular visitor of your web site and appreciate you taking the time to maintain the nice site. I will be a frequent visitor for a long time.

    Reply

  20. hidroxicloroquina 400mg [url=https://hydroxychloroquinex.com/#]coronavirus treatment[/url] hydroxychlorophine plaquemine

    Reply

  21. tadalafilo 20mg [url=https://tadalisxs.com/]tadalafil 40[/url] mambo 36 side effects tadalafil pills

    Reply

  22. for erectile dysfunction ptx [url=https://plaquenilx.com/]hydroxychloroquin[/url] erectile booster method book hydrochlorothiazide 25 mg

    Reply

  23. tadalafil peptides [url=https://tadalisxs.com/]generic tadalafil united states[/url] tadalafil 60mg order tadalafil

    Reply

  24. erectile tissue quizlet [url=https://plaquenilx.com/]hydroxychloroquine generic name[/url] is erectile dysfunction a va disability rates side effects for plaquenil

    Reply

  25. Very good article. I am dealing with a few of these issues as well..

    Reply

  26. [url=http://supersildenafil.com/]generic sildenafil from canada[/url] [url=http://sildenafilxxx.com/]generic sildenafil soft tabs[/url] [url=http://reqviagra.com/]buy viagra mexico[/url] [url=http://tadalafil24h.com/]tadalafil 7mg[/url] [url=http://viagrasmo.com/]can i buy viagra online in australia[/url]

    Reply

  27. [url=http://5000lending.com/]internet loan[/url]

    Reply

  28. [url=http://tadalafilordering.com/]100 mg tadalafil[/url] [url=http://metformingl.com/]metformin how to buy[/url] [url=http://viagrastrong.com/]buy sildenafil 100mg[/url] [url=http://cialisgmed.com/]cheapest cialis[/url] [url=http://cialisohot.com/]how to get cialis[/url]

    Reply

  29. [url=https://upcialis.com/]prescription cost for cialis[/url]

    Reply

  30. [url=https://viagradex.com/]viagra gold[/url] [url=https://mbpills.com/]motilium 20 mg[/url] [url=https://cialisztabs.com/]generic cialis 2018 prices[/url]

    Reply

  31. [url=http://pharmacyvi.com/]best online thai pharmacy[/url]

    Reply

  32. [url=http://ivermectinfast.com/]ivermectin 0.08%[/url]

    Reply

  33. [url=http://allcialis.com/]cialis for sale without prescription[/url] [url=http://cialissd.com/]cialis pharmacy coupon[/url] [url=http://viagrapos.com/]how to get viagra usa[/url] [url=http://levitraedpills.com/]levitra mexico pharmacy[/url] [url=http://hydroxychloroquine247.com/]hydroxychloroquine sulfate 300 mg[/url]

    Reply

  34. [url=https://levitraedpills.com/]order levitra online uk[/url]

    Reply

  35. [url=http://lynxpharmacy.com/]canadian pharmacy world coupon[/url] [url=http://cialisdnp.com/]can you buy cialis otc[/url] [url=http://cialiseli.com/]how to get cialis from canada[/url] [url=http://tadalafillowcost.com/]tadalafil 20 coupon[/url] [url=http://viagraedpill.com/]buy viagra from canada online[/url] [url=http://supersildenafil.com/]sildenafil 100 mg tablet usa[/url] [url=http://viagrafour.com/]how much is over the counter viagra[/url] [url=http://cialismstore.com/]buy cialis canada canadian drugstore[/url] [url=http://cialisktab.com/]40 mg cialis online[/url] [url=http://viagra100x.com/]order viagra united states[/url]

    Reply

  36. [url=http://modafinilwithoutrx.com/]provigil online purchase[/url] [url=http://viagranmed.com/]viagra capsule price in india[/url] [url=http://cialisnrg.com/]canadian pharmacy cialis 5 mg[/url] [url=http://viagrart.com/]generic viagra online europe[/url] [url=http://a4drugstore.com/]online pharmacy australia paypal[/url] [url=http://valcialis.com/]buy generic cialis usa[/url] [url=http://mbpills.com/]motilium pills[/url] [url=http://pharmacyheb.com/]canadian pharmacy[/url] [url=http://ivermectinfast.com/]stromectol 3 mg tablet[/url] [url=http://cialisnri.com/]tadalafil 100mg[/url]

    Reply

  37. [url=https://cialissd.com/]tadalafil 75 mg[/url]

    Reply

  38. [url=http://maraloans.com/]payday loan no fax[/url] [url=http://taulending.com/]loans in athens ga[/url]

    Reply

  39. [url=http://cialisdnp.com/]buy cialis 20mg online canada[/url]

    Reply

  40. [url=https://cialisztabs.com/]compare cialis[/url] [url=https://zbcialis.com/]cheap generic cialis uk[/url]

    Reply

  41. [url=https://peleloans.com/]payday loans kansas city[/url]

    Reply

  42. [url=http://cialisdist.com/]cialis tablets purchase[/url] [url=http://afdviagra.com/]can you buy viagra over the counter australia[/url] [url=http://modafinilwithoutrx.com/]modafinil online canadian pharmacy[/url] [url=http://sildenafilsuper.com/]sildenafil tablets 100mg uk[/url] [url=http://cialisohot.com/]best generic cialis[/url]

    Reply

  43. [url=https://cialisgmed.com/]average cost of 20mg cialis[/url] [url=https://cialismstore.com/]generic cialis 2017 usa[/url] [url=https://pmcialis.com/]how to order cialis from india[/url] [url=https://viagraunf.com/]how to order viagra online[/url] [url=https://levitraedpills.com/]levitra professional[/url] [url=https://lynxpharmacy.com/]india pharmacy mail order[/url] [url=https://tadalafilmstore.com/]tadalafil generic[/url] [url=https://mbpills.com/]motilium pharmacy[/url]

    Reply

  44. [url=http://shorttermloansapr.com/]personal loan contract template[/url]

    Reply

  45. [url=https://cialiseli.com/]tadalafil medication[/url] [url=https://cialiselilly.com/]generic cialis from uk[/url] [url=https://cialisworx.com/]cialis from us pharmacy[/url] [url=https://zbcialis.com/]cialis 800mg[/url] [url=https://mbpills.com/]buy motilium online uk[/url] [url=https://viagraedpill.com/]compare viagra[/url] [url=https://pharmacyheb.com/]cross border pharmacy canada[/url] [url=https://a4drugstore.com/]order pharmacy online egypt[/url] [url=https://ivermectinfast.com/]ivermectin topical[/url] [url=https://viagradex.com/]viagra pills for sale[/url]

    Reply

  46. [url=https://lynxpharmacy.com/]pharmacy wholesalers canada[/url]

    Reply

Leave a reply

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