WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 5 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:

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

Download all attachments as: .zip

Change History (9)

@bradyvercher
6 years ago

#1 @scribu
6 years ago

  • Cc scribu added

#2 @SergeyBiryukov
6 years ago

  • Version changed from trunk to 3.5

Related: [22103] (for #19729).

#3 @wonderboymusic
5 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
5 years ago

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

Seems good. Needs unit tests.

@gradyetc
5 years ago

#5 @gradyetc
5 years ago

Refreshed patch, added unit tests.

#6 @gradyetc
5 years ago

  • Cc mike@… added

#7 @wonderboymusic
5 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.