I've written a function to get products by price range. All works, but now I need add a extra meta key, that will be like 50 - 100 and featured, but code is not returning any products. What is wrong on this code?

    function product_price_filter_box($attr) {
    $limit = intval($attr['limit']);
    $min = intval($attr['min']);
    $max = intval($attr['max']);
    $query = array(
        'post_status' => 'publish',
        'post_type' => 'product',
        'posts_per_page' => $limit,
        'meta_query' => array(
            'relation'      => 'AND',
                'key' => '_price',
                'value' => array($min, $max),
                'compare' => 'BETWEEN',
                'type' => 'NUMERIC'
                'key'       => 'featured',
                'value'     => '1',
                'compare'   => '=',

    $wpquery = new WP_Query($query);
    while ( $wpquery->have_posts() ) : $wpquery->the_post(); global $product;
        wc_get_template_part( 'content', 'product' );

add_shortcode( 'product_price_filter_box', 'product_price_filter_box' );

The featured product's meta key is _featured, but you are using featured in your meta query. This will return no products, since the key doesn't exist.

Also, as far as I know, the value of the key is yes, so your arguments should be like this:

    'key'       => '_featured',
    'value'     => 'yes',

Another note, is to use the proper way to get the shortcode's attribute. You can do so by using shortcode_atts() function. Here is the syntax for your case:

$atts = shortcode_atts(
        'limit' => '20',
        'min' => '1'
        'max' => '2'

$limit = intval($atts['limit']);
$min = intval($atts['min']);
$max = intval($atts['max']);

You might want to limit the maximum posts a user can get. This can be done by using the min() function:

$limit = min(20, $limit);

And a final note. If you are using WP_Query, you should use wp_reset_postdata(); instead of wp_reset_query();, which is used after you use query_posts();.

  • 1
    ps: we could add that OP is missing the default attribute handling, range validation and is not returning the html. – birgire Aug 10 '17 at 8:40
  • Hi, Sorry I don't understand. But he mentioned that his range validation works fine? – Jack Johansson Aug 10 '17 at 8:42
  • 2
    e.g. the limit here can be anything, so if there are 1M posts then the user can fetch 1M posts through the shortcode and crash the site. Also if the attributes are missing then there's a PHP notice. Also the shortcode should return the output, not echo it. – birgire Aug 10 '17 at 8:46
  • 1
    Ah you mean validation of parameters :) Sure, I'll add that too. – Jack Johansson Aug 10 '17 at 8:46
  • That is only example, custom meta key can bet anything. Without custom meta key, code is working normaly, so i need that code also take meta key. – Foxsk8 Aug 11 '17 at 17:48

Since WC 3.0.0

You can use wc_get_min_max_price_meta_query(). Woocommerce will create the price min/max meta_query for you. This also includes a tax check.

More info here.

Below an example that matches the OP question:

$meta_query = array(
  'relation'    => 'AND',
    'key'       => 'your_custom_meta_key',
    'value'     => '1',
    'compare'   => '=',
$meta_query[] = wc_get_min_max_price_meta_query(array(
  'min_price' => 100,
  'max_price' => 200,
$query = array(
  'post_status'     => 'publish',
  'post_type'       => 'product',
  'posts_per_page'  => -1,
  'meta_query'      => $meta_query,
$wpquery = new WP_Query($query);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.