WordPress.org

Make WordPress Core

Opened 16 months ago

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

Download all attachments as: .zip

Change History (9)

bradyvercher16 months ago

comment:1 scribu16 months ago

  • Cc scribu added

comment:2 SergeyBiryukov16 months ago

  • Version changed from trunk to 3.5

Related: [22103] (for #19729).

comment:3 wonderboymusic8 months ago

  • Milestone changed from Awaiting Review to 3.7

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

comment:4 nacin7 months ago

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

Seems good. Needs unit tests.

gradyetc6 months ago

comment:5 gradyetc6 months ago

Refreshed patch, added unit tests.

comment:6 gradyetc6 months ago

  • Cc mike@… added

comment:7 wonderboymusic5 months 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.