Exclude a specific category on shop page

Hi,
I’m having some issues with my shop page display. I would like to exclude one of the categories on my shop page. Specifically in the list of categories at the top of the page. I have tried adding the following to my child theme functions.php with no success.
/**

  • Exclude products from a particular category on the shop page
    */
    function custom_pre_get_posts_query( $q ) {

    $tax_query = (array) $q->get( ‘tax_query’ );

    $tax_query[] = array(
    ‘taxonomy’ => ‘product_cat’,
    ‘field’ => ‘slug’,
    ‘terms’ => array( ‘clothing’ ), // Don’t display products in the clothing category on the shop page.
    ‘operator’ => ‘NOT IN’
    );

    $q->set( ‘tax_query’, $tax_query );

}
add_action( ‘woocommerce_product_query’, ‘custom_pre_get_posts_query’ );

However I have a php file in a woocommerce folder on the same level as the function.php file which contains a file called “content-product_cat.php” which I believe is controlling the shop page view. However I am not sure if the above code would work here.
I know often wooCommerce issues are outside your realm, but I sure would appreciate some assistance considering its inside the unique structure of the theme and its files.
Let me know what you think.
Cheers
David

Hi,
I have been tinkering and found that making books a subcategory of another parent category did the trick for me. it no longer displays on the shop page as a category.
Cheers
D

Thanks for sharing David! Yes, setting it as child category should do it since mostly the top level category is displayed.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.