A powerful membership solution for WordPress

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:

Post Promoter Pro

Posted by Chris Klosowski

Hi, I'm Chris Klosowski. Currently I am a Lead Developer of Easy Digital Downloads, where we build the easiest way to sell digital products with WordPress.

I am also the person behind Post Promoter Pro, the most effective way to promote your WordPress Content.

15 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

Leave a reply

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