[wp-trac] [WordPress Trac] #39540: NOT EXISTS meta condition doesn't work if meta has NULL value.

WordPress Trac noreply at wordpress.org
Thu May 15 13:09:44 UTC 2025


#39540: NOT EXISTS meta condition doesn't work if meta has NULL value.
------------------------------------------+-----------------------------
 Reporter:  avahura                       |       Owner:  (none)
     Type:  defect (bug)                  |      Status:  new
 Priority:  normal                        |   Milestone:  Future Release
Component:  Query                         |     Version:  4.7
 Severity:  normal                        |  Resolution:
 Keywords:  needs-patch needs-unit-tests  |     Focuses:
------------------------------------------+-----------------------------

Comment (by vishalkakadiya):

 @avahura I checked this issue, I'm also able to reproduce this issue, but
 there is a good reason for it not to query on **meta_value**, as by
 default, the **meta_value** field does not have indexing, which makes the
 DB query slower than just checking **meta_key** exists. If we add that
 condition to the core, then it makes all the NOT EXISTS queries slower.
 The best practice is not to save an empty or null meta record itself,
 which makes the DB size smaller and queries faster.

 Still, if someone needs to check **NOT EXISTS** and **Empty** conditions
 together, can use the below snippet:


 {{{
 $args = [
         'post_type'      => 'post', // Change to your specific post type
 if needed
         'posts_per_page' => 20,     // Fetch all posts
         'meta_query'     => [
                 'relation' => 'OR',
                 [
                         'key'     => '_example_meta',
                         'compare' => 'NOT EXISTS',
                 ],
                 [
                         'key'     => '_example_meta',
                         'compare' => '=',
                         'value'   => '',
                 ],
         ],
 ];

 $query = new \WP_Query( $args );
 }}}

 CC @dd32 @whyisjake can you guys suggest next steps on this ticket?

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/39540#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list