Make WordPress Core

Opened 9 years ago

Closed 8 years ago

#22967 closed defect (bug) (fixed)

Null value in meta query changes the type of comparison

Reported by: bradyvercher Owned by: wonderboymusic
Milestone: 3.8 Priority: normal
Severity: normal Version: 3.5
Component: Query Keywords: has-patch needs-unit-tests
Focuses: Cc:


If a null value is set in a meta query, it effectively returns the same results as setting the 'compare' arg to 'EXISTS', even if it's explicitly set to the default '='.

For example:

$value_var = null;
meta_query => array(
            'key'     => '_my_meta_key',
            'value'   => $value_var,
            'compare' => '='

That returns all rows where '_my_meta_key' exists and ignores 'value' instead of treating it like an empty string. This can be troublesome where the value is passed in dynamically like the example, so validation would have to be done prior to every meta query to ensure the value isn't null.

I've attached a patch for review that checks for the existence of the 'value' key rather than using isset and casts it to a string if it's null.

Attachments (2)

22967.patch (921 bytes) - added by bradyvercher 9 years ago.
22967.2.patch (1.6 KB) - added by gradyetc 8 years ago.

Download all attachments as: .zip

Change History (9)

9 years ago

#1 @scribu
9 years ago

  • Cc scribu added

#2 @SergeyBiryukov
9 years ago

  • Version changed from trunk to 3.5

Related: [22103] (for #19729).

#3 @wonderboymusic
8 years ago

  • Milestone changed from Awaiting Review to 3.7

Patch still applies, moving to 3.7 to live with its meta query cleanup brethren

#4 @nacin
8 years ago

  • Keywords needs-unit-tests added
  • Milestone changed from 3.7 to 3.8

Seems good. Needs unit tests.

8 years ago

#5 @gradyetc
8 years ago

Refreshed patch, added unit tests.

#6 @gradyetc
8 years ago

  • Cc mike@… added

#7 @wonderboymusic
8 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 26053:

Respect the compare operator value in meta_query when value evaluates to null. Adds Unit Test.

Props bradyvercher, gradyetc.
Fixes #22967.

Note: See TracTickets for help on using tickets.