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:
- 9000000
- 900
- 8500000
- 750
- 600
- 15000
I would like them to be ordered (DESC) as follows:
- 9000000
- 8500000
- 15000
- 900
- 750
- 600
Am I doing something wrong here?
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.