Skip to content

Instantly share code, notes, and snippets.

@braddalton
Last active December 14, 2023 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save braddalton/9d4d7b4a0067177670a12e756a3e274b to your computer and use it in GitHub Desktop.
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
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',
),
));
}
}
// 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