Start a project
WooCommerce

Hide empty product categories from Main Navigation in WooCommerce

· by Daniel

Hide empty product categories from Main Navigation in WooCommerce
You can of course add unlimited WooCommerce Product Categories into your main navigation. But sometimes they get empty because of Stock change or similar. If you automatically want to hide empty product categories in your WordPress Navigation place the code below into your functions.php file.
add_filter( 'wp_get_nav_menu_items', 'nav_remove_empty_category_menu_item', 10, 3 );
function nav_remove_empty_category_menu_item ( $items, $menu, $args ) {
    global $wpdb;
    $nopost = $wpdb->get_col( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE count = 0" );
    foreach ( $items as $key => $item ) {
        if ( ( 'taxonomy' == $item->type ) && ( in_array( $item->object_id, $nopost ) ) ) {
            unset( $items );
        }
    }
    return $items;
}

Questions & answers (4)

  1. MENOTYOUJune 3, 2026

    Hidden everything

  2. ValmerJune 26, 2024

    in fact, this code allows me to hide an empty category. The problem is that when this same category fills up again, it is automatically placed at the bottom of the menu in “Appearance > Menu. I would like it not to move in position. Can you help me?

  3. ValmerJune 20, 2024

    Good morning, I integrated this same code to hide a category when it is empty and it works well. But I would like it to be positioned in the same place when a product is added again.
    Today, it is positioned at the very bottom of the menu.
    Do you have a solution to hide this category unless it moves around?
    Thank you for your help

  4. Jon-Paul SmithApril 17, 2022

    Hi there,
    Is it possible to hide product categories and / or subcategories if the products in them are out of stock please?

    So for example I have the ‘Out of stock visibility – Hide out of stock items from the catalog’ in Woocommerce settings checked, so out of stock products are not being displayed.

    However I also have a category for ‘Elektra Comic Books’ that has two subcategories:

    Elektra Vol 3 (2014)
    Elektra: Black, White & Blood (2022)

    Elektra Vol 3 (2014) has a comic in stock, but Elektra: Black, White & Blood (2022) has no comics in stock and displays the ‘No products were found matching your selection.’ message when entered.

    Is there some code I can use so that the Elektra: Black, White & Blood (2022) subcategory (or any other subcategory that has no products in stock) is not displayed, but if new stock is added it will become visible / available again?

More articles

All articles →
WooCommerceOctober 29, 2026 EmpCo Directive: What Online Shops Must Know About Green Claims Since 27 September 2026 “Sustainable”, “climate-neutral shipping”, “eco-friendly packaging”: claims like these appear in many online shops – in product texts, banners, badges in the footer or in carbon of… WooCommerceDecember 22, 2023 Prevent WP Rocket from Caching Filtered WooCommerce pages When using WP Rocket with preloading in combination with a filter plugin like Filter Everything pro, then your database AND cache folder can explode. We had a site with only around… WooCommerceApril 20, 2022 How to Reactivate old WooCommerce PayPal Standard Gateway WooCommerce has replaced the old PayPal Standard Payment Gateway since version 5.5.

Let's increase your online sales.

Tell us about your shop or website. A developer replies, not a sales team.

★ 4.85/5 from 800+ ratings · Envato Elite Author · Made in Germany