Skip to content

Instantly share code, notes, and snippets.

@yratof
Created April 9, 2016 17:45
Show Gist options
  • Save yratof/0da0060b3d01220d46bd42cc4ef4657e to your computer and use it in GitHub Desktop.
Save yratof/0da0060b3d01220d46bd42cc4ef4657e to your computer and use it in GitHub Desktop.
Woocommerce sale items via URL
<?php
// Display sale items only when searching by /?sale=show
add_action('pre_get_posts','sale_items');
function sale_items($query) {
if (!is_admin() && ( $query->is_post_type_archive( 'product' ) || $query->is_tax( get_object_taxonomies( 'product' ))) && isset($_GET['sale']) && $_GET['sale'] == 'show' ) {
$meta_query = array(
array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
);
$query->set( 'meta_query' , $meta_query );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment