Make WordPress Core

Opened 7 years ago

Last modified 4 years ago

#39540 new defect (bug)

NOT EXISTS meta condition doesn't work if meta has NULL value.

Reported by: avahura's profile avahura Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 4.7
Component: Query Keywords: needs-patch needs-unit-tests
Focuses: Cc:


It seems problem exists since meta query class release.

Right now NOT EXISTS works only if meta doesn't exist at all, but if it has NULL value we got incorrect result. I understand that better not use NULL values with metas at all, but you allow to write NULL as meta value, so please add possibility to check it.

Just need to change string in class-wp-meta-query.php:

$sql_chunks['where'][] = $alias . '.' . $this->meta_id_column . ' IS NULL';

For example on:

$sql_chunks['where'][] = "$alias.meta_value IS NULL";

And both cases will be covered.

Thank you in advance.

Change History (6)

#1 @dd32
7 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to 4.8

This makes sense to me; NOT EXISTS shouldn't return rows which have literal NULLs as values, so testing if the ID column exists instead of the value column makes sense.

#2 @avahura
7 years ago

Basically i agree with you, but how to check NULL meta value with usage of WP query builder then? This should be easy. NOT EXISTS could mean meta (row) not exists or meta value not exists, but you use "compare" for values check ( "=", ">", "IN" and etc. ) and NOT EXISTS uses in "compare" as well. We just use it in different contexts.
Also you can prevent NULL writing as meta value and just remove such meta or even add "NULL" as meta_value and parse it accordingly.

Probably i don't right.

#3 @dd32
7 years ago

  • Keywords needs-patch removed
  • Milestone changed from 4.8 to Awaiting Review

My apologies, I misread your initial post.

I disagree that NULL should be matched by NOT EXISTS, as the row does exist, it just has a null value.

It doesn't look like we have a good way in meta queries to handle NULL at present though.

This ticket was mentioned in Slack in #core by noisysocks. View the logs.

4 years ago

#5 @whyisjake
4 years ago

  • Keywords needs-patch needs-unit-tests added

#6 @whyisjake
4 years ago

  • Milestone changed from Awaiting Review to Future Release
Note: See TracTickets for help on using tickets.