Skip to content

Instantly share code, notes, and snippets.

@mikejolley
Last active November 8, 2022 00:25
Show Gist options
  • Save mikejolley/409a5180632535addb93a2ac63576193 to your computer and use it in GitHub Desktop.
Save mikejolley/409a5180632535addb93a2ac63576193 to your computer and use it in GitHub Desktop.
WooCommerce 3.3 - Hide uncategorized category from the shop page on the frontend
<?php // Do not include this if already open!
/**
* Code goes in theme functions.php.
*/
add_filter( 'woocommerce_product_subcategories_args', 'custom_woocommerce_product_subcategories_args' );
function custom_woocommerce_product_subcategories_args( $args ) {
$args['exclude'] = get_option( 'default_product_cat' );
return $args;
}
@digimarkup
Copy link

digimarkup commented Nov 7, 2022

For the sidebar widget, the other solutions did not work for me. I had to remove the category id from $args[Include]

add_filter( 'woocommerce_product_categories_widget_args', 'custom_woocommerce_remove_uncategorized_category_sidebar' );

function custom_woocommerce_remove_uncategorized_category_sidebar($args)
{
   
    if (!empty($args['include']) && is_string($args['include']))
    {
        $included_categories = explode (",", $args['include']);
        $uncategorized_id = array_search(get_option('default_product_cat'), $included_categories);
        if ($uncategorized_id)
        {
            unset($included_categories[$uncategorized_id]);
            $args['include'] = implode(",", $included_categories);
        }
    }

    return $args;
}

Goes in functions.php or plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment