• Resolved karenelated

    (@karenelated)


    Hi

    I have coupons as a product category that is automatically imported so I can’t edit them individually as my edits would be overwritten every day when the datafeed is refreshed.

    I want to hide coupons from the shop page as really they aren’t products and look silly in the feed. so I used the code provided in WooCommerce docs to do that, which works.

    /**
     * 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( 'coupons-deals' ), 
               'operator' => 'NOT IN'
        );
    
        $q->set( 'tax_query', $tax_query );
    
    }
    add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );  

    But it also hides them from the coupons category page which is even worse.

    How can I hide them from the shop page, but still show them on their category page?

    Thanks
    Karen

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support con

    (@conschneider)

    Engineer

    Hi Karen,

    Here goes nothing:

    
    /**
     * Exclude products from a particular category on the shop page
     */
    function custom_pre_get_posts_query( $q ) {
        if(is_shop()) {
        $tax_query = (array) $q->get( 'tax_query' );
    
        $tax_query[] = array(
               'taxonomy' => 'product_cat',
               'field' => 'slug',
               'terms' => array( 'coupons-deals' ), 
               'operator' => 'NOT IN'
        );
        
        $q->set( 'tax_query', $tax_query );
    }
    }
    add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
    

    Kind regards,

    Thread Starter karenelated

    (@karenelated)

    Yay it worked. thank you so much!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Exclude products in a category from the shop page, but show on category page’ is closed to new replies.