Hide category on shop page

Please advise, I’m looking for a way to hide products from a certain category on a home page. I I have tried a solution from this page but it didn’t work.

https://www.hardworkingnerd.com/how-to-hide-a-category-from-the-shop-page-in-woocommerce/

  • Jack Alltrade
    • WordPress Warrior

    Standard disclaimer use at your own risk backup your functions.php file or functionality plugin, etc. etc.

    In addition to the following I also set product not to be visible in catalog (near publish/update button).

    Client where client is the slug of your category

    /** WooCommerce Hide Cat. */
    function custom_pre_get_posts_query( $q ) {
        $tax_query = (array) $q->get( 'tax_query' );
        $tax_query[] = array(
               'taxonomy' => 'product_cat',
               'field' => 'slug',
               'terms' => array( 'client' ), // Don't display products in the client category on the shop page.
               'operator' => 'NOT IN'
        );
    
        $q->set( 'tax_query', $tax_query );
    }
    add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
  • Kris Tomczyk
    • Support

    Hi Phil Samcro

    I hope you are doing good today.

    I added same code which Nastia use on chat session and it works fine:

    add_action( 'woocommerce_product_query', 'prefix_custom_pre_get_posts_query' );
    
    function prefix_custom_pre_get_posts_query( $q ) {
    
    if( is_shop() || is_page('shop') ) { // set conditions here
    $tax_query = (array) $q->get( 'tax_query' );
    
    $tax_query[] = array(
    'taxonomy' => 'product_cat',
    'field' => 'slug',
    'terms' => array( 'sdw-hoodtales' ),
    'operator' => 'NOT IN'
    );
    $q->set( 'tax_query', $tax_query );
    }
    }

    I added this in /wp-content/mu-plugins/hide_cat.php.

    Also, I removed from the Shop page WooCommerce Products module, because by default Shop page loads all products. This could be confusing during the chat session as you saw 2 same boxes with the same products. The above code works fine when those products are loaded via Woocommerce core, it does not work when you use WooCommerce Products module in page builder.

    Right now your shop page does not display this category.

    Hope this helps.

    Kind Regards,
    Kris