$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Woocommerce query by price range and custom meta key|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Woocommerce query by price range and custom meta key

matteradmin10PV0评论

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',
            array(
                'key' => '_price',
                'value' => array($min, $max),
                'compare' => 'BETWEEN',
                'type' => 'NUMERIC'
            ),
            array(
                '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' );
    endwhile;
    wp_reset_query();

}
add_shortcode( 'product_price_filter_box', 'product_price_filter_box' );

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',
            array(
                'key' => '_price',
                'value' => array($min, $max),
                'compare' => 'BETWEEN',
                'type' => 'NUMERIC'
            ),
            array(
                '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' );
    endwhile;
    wp_reset_query();

}
add_shortcode( 'product_price_filter_box', 'product_price_filter_box' );
Share Improve this question edited Aug 10, 2017 at 7:47 Johansson 15.4k11 gold badges44 silver badges80 bronze badges asked Jul 27, 2017 at 11:55 Foxsk8Foxsk8 1311 gold badge1 silver badge14 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2 +50

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:

array(
    '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(
    array(
        'limit' => '20',
        'min' => '1'
        'max' => '2'
    ), 
    $atts, 
    'product_price_filter_box' 
);

$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();.

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',
  array(
    '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);
Post a comment

comment list (0)

  1. No comments so far