Make WordPress Core

Opened 14 years ago

Closed 13 years ago

#22967 closed defect (bug) (fixed)

Null value in meta query changes the type of comparison

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

Description

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(
        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 14 years ago.
22967.2.patch​ (1.6 KB ) - added by gradyetc 13 years ago.

Download all attachments as: .zip

Change History (9)

#1 @scribu
14 years ago

  • Cc scribu added

#2 @SergeyBiryukov
14 years ago

  • Version trunk → 3.5

Related: [22103] (for #19729).

#3 @wonderboymusic
13 years ago

  • Milestone Awaiting Review → 3.7

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

#4 @nacin
13 years ago

  • Keywords needs-unit-tests added
  • Milestone 3.7 → 3.8

Seems good. Needs unit tests.

#5 @gradyetc
13 years ago

Refreshed patch, added unit tests.

#6 @gradyetc
13 years ago

  • Cc mike@… added

#7 @wonderboymusic
13 years ago

  • Owner set to wonderboymusic
  • Resolution → fixed
  • Status new → 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.