Opened 2 years ago
Last modified 23 months ago
#17235 new defect (bug)
meta_query fails if you don't pass in an array of arrays
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Query | Version: | 3.1 |
| Severity: | normal | Keywords: | 2nd-opinion has-patch |
| Cc: | batmoo |
Description
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', } ), ), );
Attachments (4)
Change History (13)
comment:1
in reply to:
↑ description
greuben — 2 years ago
- Keywords close added
Indeed, the meta_query will just be ignored.
I think handling single arrays would create more confusion than solve, especially since you also have the old 'meta_key' and 'meta_value' query vars.
What if a user does something like this:
$my_query = WP_Query( array array ( 'post_type' => 'post', 'meta_query' => array ( 'key' => 'foo', 'value => 'some_val', array( 'key' => 'bar', 'value' => 'another_val', ) ), ), );
meta_query should ideally support the simplified version. (Same with tax_query.)
If someone mixes the simplified version with the non-simplified version, then we should just take the root-level (simplified) version.
Replying to nacin:
meta_query should ideally support the simplified version. (Same with tax_query.)
If someone mixes the simplified version with the non-simplified version, then we should just take the root-level (simplified) version.
I agree but I think we should use non-simplified(array of arrays) because the person using non-simplified version is aware of the meta_query syntax.
Replying to greuben:
Replying to nacin:
If someone mixes the simplified version with the non-simplified version, then we should just take the root-level (simplified) version.
I agree but I think we should use non-simplified(array of arrays) because the person using non-simplified version is aware of the meta_query syntax.
Regardless of the approach, if a dev is mixing the two, it might be worth adding a _doing_it_wrong to notify them that their meta_query syntax is incorrect and should be fixed.
Patch allows meta_query to work with single arrays. Defaults to simplified version if both simple and complex versions are passed in.

Replying to batmoo:
"svn up" and it doesn't generate funky queries anymore.