Last active
December 14, 2023 13:04
-
-
Save braddalton/9d4d7b4a0067177670a12e756a3e274b to your computer and use it in GitHub Desktop.
WooCommerce Exclude On Sale Products from Shop Page https://wpsites.net/?p=114677
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('pre_get_posts', 'wc_exclude_sale_items_from_shop_page'); | |
function wc_exclude_sale_items_from_shop_page($query) { | |
if ( is_admin() || ! $query->is_main_query() ) { | |
return; | |
} | |
// Check if it's the main shop page | |
if ( is_shop() ) { | |
$query->set('meta_query', array( | |
array( | |
'key' => '_sale_price', | |
'compare' => 'NOT EXISTS', | |
), | |
)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add the following code to your theme's functions.php file or a custom plugin. | |
add_filter('woocommerce_product_query_meta_query', 'woo_exclude_sale_items_from_shop_page'); | |
function woo_exclude_sale_items_from_shop_page($meta_query) { | |
if ( is_admin() || ! is_shop() ) { | |
return $meta_query; | |
} | |
$meta_query[] = array( | |
'key' => '_sale_price', | |
'compare' => 'NOT EXISTS', | |
); | |
return $meta_query; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment