1

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?

3
  • What version of WordPress are you running? The meta_value_num option wasn't added until 3.0. So if you're still using 2.9, it would make sense that meta values are compared as strings. Nov 7, 2010 at 18:05
  • I am definitely running version 3.0.1
    – jnthnclrk
    Nov 7, 2010 at 22:41
  • Any ideas anyone?
    – jnthnclrk
    Nov 8, 2010 at 1:41

1 Answer 1

1

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.

5
  • Damn, so it would seem to be a bug, as the codex suggests you can use meta_compare with integers. Has this been reported in Trac? Is there a fix/ workaround that you know of?
    – jnthnclrk
    Nov 8, 2010 at 9:40
  • Sorry, I don't hang out on Trac so no idea about status. As for fix I didn't test myself but best bet is probably filter posts_where to unquote string.
    – Rarst
    Nov 8, 2010 at 9:47
  • Right, any hints on how to do that?
    – jnthnclrk
    Nov 8, 2010 at 10:26
  • Been doing a lot of Googling, and whilst I find a few examples of how to use posts_where, nothing relates to this specific problem...
    – jnthnclrk
    Nov 8, 2010 at 10:48
  • Posted a new question: wordpress.stackexchange.com/questions/3765/…
    – jnthnclrk
    Nov 8, 2010 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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