I’m looking to exclude a Category from the Shop home page of my woocommerce store.
The loop I am using is the default loop internal to the plugin. I am not using a shortcode to call in the loop, nor am I using a custom php query.
The Shop home page is set to display every category. From there the user clicks through the various child categories until the appropriate products are revealed.
I have crawled through every conceivable solution within this website, none of which have has any effect – I’m hoping someone can help.
I want the exclude the following category:
Category Name: Z_PRODUCT CATALOGUE
ID: 122
Slug: product-catalogue
Examples of code I have attempted to use but failed:
function se_customize_product_shortcode( $args, $atts ) {
if ( is_page( 'products' ) ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'product-catalogue' ),
'operator' => 'NOT IN'
)
);
}
return $args;
}
add_filter( 'woocommerce_shortcode_products_query', 'se_customize_product_shortcode', 10, 2 );
and
add_filter( 'pre_get_posts', 'custom_query_exclude_products', 900, 1 );
function custom_query_exclude_products( $query ) {
if ( is_admin() || is_search() || is_shop() ) return $query;
$query->set( 'tax_query', array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'product-catalogue' ),
'operator' => 'NOT IN'
) ) );
remove_action( 'pre_get_posts', 'custom_query_exclude_products' );
return $query;
}
2
Answers
Unfortunatly this did not work for me. I have however found a soltuion which worked.
This snippet works for me. It’s based on this woocommerce support article. You need to put the slug for your excluded category in the
terms
array; double check that you have the right slug.I use the
woocommerce_product_query
action hook, but you try to use thewoocommerce_shortcode_products_query
filter hook.It generally takes about six years at the WordPress equivalent of the Hogwarts Academy of Magic to learn what each arcane hook does.