phpwordpresswoocommerceproduct

Show only featured products in Woocommerce shop page


I want to display in default shop page from WooCommerce only featured products, nothing more... It's there a solution to display in WooCommerce shop template only the featured products?


Solution

  • You should use this custom function hooked in woocommerce_product_query_tax_query filter hook, that will display only featured product in shop (but not in other archives pages):

    // Display featured products in shop pages
    add_filter( 'woocommerce_product_query_tax_query', 'custom_product_query_tax_query', 10, 2 );
    function custom_product_query_tax_query( $tax_query, $query ) {
        if( is_admin() ) return $tax_query;
    
        if ( is_shop() ) {
            $tax_query[] = array(
                'taxonomy' => 'product_visibility',
                'field'    => 'name',
                'terms'    => 'featured'
            );
        }
    
        return $tax_query;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.