1

I am trying to get posts by price using query_post() function. Price must be in range between two values. This I made using meta_query. But when I cant get posts when right value higher then 999 in 'value' => array(0, 999). When it is lower all fine. Where I am wrong?

I have products with price value = 100. So maybe during query execution 1000 interpreted as 100? Also I have products with prices = 240. When I am setting 'value' => array(0, 2300) I cant get products with prices 240.

$args = array(
    'post_type'        => 'product',
    'product-category' => get_query_var('product-category'),
    'orderby'          => 'wpcf-product-price',
    'order'            => 'ASC',
    'meta_query'       => array(
        array(
        'key'      => 'wpcf-product-price',
        'value'    => array(0, 1000),
        'compare'  => 'BETWEEN',
        ),
    ),
);
query_posts( $args );

Here are dump of $wp_query:

  ...["meta_query"]=>
  object(WP_Meta_Query)#4450 (2) {
    ["queries"]=>
    array(1) {
      [0]=>
      array(3) {
        ["key"]=>
        string(18) "wpcf-product-price"
        ["value"]=>
        array(2) {
          [0]=>
          int(0)
          [1]=>
          int(10000)
        }
        ["compare"]=>
        string(7) "BETWEEN"
      }
    }
    ["relation"]=>
    string(3) "AND"
  }
  ["date_query"]=>
  bool(false)
  ["request"]=>
  string(565) "SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1  AND ( wp_term_relationships.term_taxonomy_id IN (3,6,7,8) ) AND wp_posts.post_type = 'product' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND ( (wp_postmeta.meta_key = 'wpcf-product-price' AND CAST(wp_postmeta.meta_value AS CHAR) BETWEEN '0' AND '10000') ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date ASC LIMIT 0, 10"
  ["posts"]=>
  array(1) ...{

As you can see value in BETWEEN is 10000 but I get 1 product: ...["posts"]=> array(1) {.... When value is 9999 then I have all products: ...["posts"]=> array(5) {...

| improve this question | | | | |
2

I found solution by using type parameter in meta_query, set it to numeric. By default it is char and 1000 interpreted as string.

'meta_query'       => array(
    array(
    'key'      => 'wpcf-product-price',
    'value'    => array(0, 1000),
    'type'     => 'numeric',
    'compare'  => 'BETWEEN',
    ),
),
| improve this answer | | | | |
  • Did you mean "As by default..." instead of "So by default..."? – Mark Kaplun Oct 3 '14 at 8:15
  • "So" removed :) – Alexander Oct 3 '14 at 11:52

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.