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: batmoo 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)

17235.diff (749 bytes) - added by greuben 2 years ago.
17235-2.diff (693 bytes) - added by batmoo 23 months ago.
17235-meta.diff (508 bytes) - added by batmoo 23 months ago.
17235-taxonomy.diff (508 bytes) - added by batmoo 23 months ago.

Download all attachments as: .zip

Change History (13)

comment:1 in reply to: ↑ description   greuben2 years ago

Replying to batmoo:

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.

"svn up" and it doesn't generate funky queries anymore.

  • 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',
			)
		),
	),
);

comment:3 follow-up: ↓ 5   nacin2 years ago

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.

  • Keywords 2nd-opinion added; close removed

comment:5 in reply to: ↑ 3 ; follow-up: ↓ 7   greuben2 years ago

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.

  • Keywords has-patch added

greuben2 years ago

comment:7 in reply to: ↑ 5   batmoo2 years ago

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.

Ignore 17235-2.diff. New patch is much cleaner. Also attached is a patch that allows single arrays for tax queries as well.

Note: See TracTickets for help on using tickets.