I am trying to hide all the sale products on the homepage that is displaying products using Woocommerce shortcode. I am new on here and after searching high and low, I couldn't find a solution.
I have tried to use Hide all on sale products on Woocommerce shop page answer code and it works on shop page.
Is there a way for this code to be applied to the homepage instead of the shop page?
I tried this slightly modified version:
add_filter( 'woocommerce_product_query_meta_query', 'on_sale_products_not_in_archives', 10, 2 );
function on_sale_products_not_in_archives( $meta_query, $query ) {
// For woocommerce shop pages
if( is_page( 87 ) ){
$meta_query[] = array(
'key' => '_sale_price',
'value' => '',
'compare' => '=',
);
}
return $meta_query;
}
But it didn't work.
Any help on this is appreciated.
To filter the product query with shortcodes, you need to use woocommerce_shortcode_products_query
filter hook, but this will work on all products except variable products.
The code (targeting home page only):
add_filter( 'woocommerce_shortcode_products_query', 'hide_on_sale_products_in_home', 50, 3 );
function hide_on_sale_products_in_home( $query_args, $atts, $loop_name ){
if( is_front_page() ){
$query_args['meta_query'] = array( array(
'key' => '_sale_price',
'value' => '',
'compare' => '=',
) );
}
return $query_args;
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.