Question

Trying to use meta_compare as suggested in the codex:

query_posts('meta_key=miles&meta_compare=<=&meta_value=22');

Here is my code:

global $wp_query;
query_posts(
array_merge(
    array(
        'category__and' => $mycatsarray,
        'meta_key' => 'price',
        'meta_compare' => '>=',
        'meta_value' => 8500000,
        'orderby' => meta_value_num,
        'order'=>DESC
    ),
    $wp_query->query
)

);

I'm using array_merge to persist the original query. So I couldn't quite figure out how to use the suggested syntax ('&name=value') for meta_compare and meta_value. Anyways, in my code the meta_value seems to be treated like a string and not an integer as expected.

Here is an example of some typical price custom fields from my custom posts:

  • 8500000
  • 600
  • 15000
  • 900
  • 750
  • 9000000

If it's not a string issue, it could be that WordPress is ordering (DESC) these custom fields as follows:

  1. 9000000
  2. 900
  3. 8500000
  4. 750
  5. 600
  6. 15000

I would like them to be ordered (DESC) as follows:

  1. 9000000
  2. 8500000
  3. 15000
  4. 900
  5. 750
  6. 600

Am I doing something wrong here?

Was it helpful?

Solution

See this question and my answer there query_posts ->using meta_compare / where meta value is smaller or greater or equals…

Basically for the purpose of meta comparison value is always treated as string, because it is passed as such to $wpdb->prepare() method.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top