I have a query that returns all of my custom post types, ordered by the correct field. What I’m now trying to do is add a filter to it; the only new item I’m introducing is the meta_compare and meta_value_num fields.
Here’s what my query looks like:
array(9) {
["post_type"]=> string(16) "my_custom_post_type"
["post_status"]=> string(7) "publish"
["posts_per_page"]=> int(9)
["paged"]=> int(2)
["meta_key"]=> string(3) "age"
["meta_value_num"]=> array(2) {
[0]=> int(3)
[1]=> int(4)
}
["meta_compare"]=> string(1) "="
["orderby"]=> string(14) "meta_value_num"
["order"]=> string(3) "ASC"
}
Even if I make the meta_value_num a single int, instead of being an array, it still always returns all items, not just the ones for the specified age.
UPDATE
I’ve also tried this array, based on a recommendation below, but no luck with it either. It still returns all posts of the custom post type.
array(5) {
["post_type"]=>string(16) "my_custom_post_type"
["post_status"]=>string(7) "publish"
["posts_per_page"]=>int(9)
["paged"]=>int(0)
["meta_query"]=>array(4) {
["key"]=>string(3) "age"
["value"]=>array(3) {
[0]=>int(2)
[1]=>int(3)
[2]=>int(4)
}
["compare"]=>string(2) "IN"
["type"]=>string(7) "NUMERIC"
}
}
‘meta_query’ must be an array. That array contains an array with key, value and compare (and optionally type). This allows meta_query to have multiple queries in it (like age=5, color=red).
So the reason this was not working is that meta_query has to have two arrays.