WordPress.org

Make WordPress Core

Opened 10 months ago

Last modified 3 months ago

#24576 new enhancement

Make WP_Meta_Query accept arrays for keys

Reported by: Viper007Bond Owned by:
Milestone: Future Release Priority: lowest
Severity: trivial Version: 3.5.1
Component: Query Keywords: needs-patch meta-query needs-unit-tests
Focuses: Cc:

Description

#20312 touches on this but I thought of a much better solution for more complicated logic while still sticking to the API: allow the key to be an array which would inverse the overall relation value:

For example, say you want this:

( key1 = abc OR key2 = abc ) AND key3 = def

You could simply do this:

'meta_query' => array(
	'relation' => 'AND',
	array(
		'key' => array( 'key1', 'key2' ),
		'value' => 'abc',
	),
	array(
		'key' => 'key3',
		'value' => 'def',
	),
),

Or for this:

( key1 = abc AND key2 = abc ) OR key3 = def

You'd just switch the relation to OR.

Certainly edge case but I'm throwing it out there because I hate using filters to hack away at the SQL directly. :)

Change History (3)

comment:1 rmccue10 months ago

+1. Would this allow the value to be an equal sized array for ( key1 = abc OR key2 = def )? (That probably introduces extra complexity, but gives it similar behaviour to str_replace())

comment:2 wonderboymusic5 months ago

  • Keywords meta-query added

comment:3 wonderboymusic3 months ago

  • Keywords needs-unit-tests added
  • Milestone changed from Awaiting Review to Future Release
Note: See TracTickets for help on using tickets.