Include only specific categories in WooCommerce product categories widget

3.6k views Asked by At

I'm using WooCommerce plugin for Wordpress. It comes with a widget called WooCommerce Product Categories which can display a drop-down of all your product categories. I have searched online and found the following code which will exclude certain categories from appearing, categories with ID 16 and 20 in this snippet:

add_filter( 'woocommerce_product_categories_widget_args', 'woo_product_cat_widget_args' );

function woo_product_cat_widget_args( $cat_args ) { 
$cat_args['exclude'] = array('16','20');
return $cat_args;
}

What I need is the opposite. I want a filter/function similar to above, but which enables me to specify which categories to include - i.e. exclude everything but the IDs that I specify.

1

There are 1 answers

0
user3213174 On

You can try this;

add_filter( 'woocommerce_product_categories_widget_args', 'woo_product_cat_widget_args' );

function woo_product_cat_widget_args( $cat_args ) { 
    $cat_args['include'] = array('16','20');
    return $cat_args;
}

actually you can use any of these arguments that listed on this page https://codex.wordpress.org/Template_Tags/wp_list_categories

Hope that helps!