Tagged: category, shop, woocommerce
I found another thread here from a different theme that suggested adding some code to the functions.php file and I tried that but it didn’t seem to do anything. I have a category that is showing up in my shop but I need to exclude it from there.
I found this article: http://www.wptaskforce.com/how-to-exclude-one-or-more-category-in-woocommerce-shop-page/
Basically you need to insert it at the bottom of functions.php. Then replace
'field' => 'slug',
'terms' => array( 'PUT YOUR CATEGORY HERE' ), // Don't display products in the m
with
'field' => 'id',
'terms' => array(20,25), // Don't display products in the m
and instead of 20, 25 insert the category id(s) you want to exclude. Separate them with a comma.
I tried entering that but nothing seems to happen. I’m still seeing the categories I want to hide. I put it in the functions.php in the theme, and then tried in in woocommerce-core-funtions.php, but nothing changes. Am I doing something wrong?
Not sure why it doesn’t work in your case. I tested it with
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q )
{
if (!$q->is_main_query() || !is_shop()) return;
if ( ! is_admin() )
{
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => array( 57 ),
'operator' => 'NOT IN'
)));
}
}
(57 is the id of the category I excluded) and it worked for me. If it doesn’t work try to deactivate all third party plugins/extensions except WooCommerce – maybe another plugin hooks into the query and breaks it.