WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#39540 new defect (bug)

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

Reported by: avahura Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7
Component: Query Keywords:
Focuses: Cc:
PR Number:

Description

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 (3)

#1 @dd32
3 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
3 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
3 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.

Note: See TracTickets for help on using tickets.