meta_query fails if you don't pass in an array of arrays
|Reported by:||batmoo||Owned by:|
This tripped me up the first time I used meta_query (see #16563) and I've seen others fall into the same trap. If you don't pass in an array of arrays into meta_query, it generates some funky (but valid) SQL and fails to return anything.
The main instance where people will fall into this is if they only have a single key/value pair to look for. In this case, passing in an array of arrays does not seem intuitive, and meta_query should be smart enough to work with either. Examples below.
This doesn't work:
$my_query = WP_Query( array array ( 'post_type' => 'post', 'meta_query' => array ( 'key' => 'my_key', 'value' => 'my_value', ), ), );
But this does:
$my_query = WP_Query( array array ( 'post_type' => 'post', 'meta_query' => array ( array( 'key' => 'my_key', 'value' => 'my_value', } ), ), );
Change History (22)
comment:10 wonderboymusic — 4 months ago
- Keywords 2nd-opinion removed
- Milestone changed from Awaiting Review to 3.7