Support

Account

Home Forums General Issues meta_query returns false if field doesn\\\'t exist

Helping

meta_query returns false if field doesn\\\'t exist

  • The following query returns false if the key is not set. Given I am trying to add the feature and would prefer not to have to change every post, this seems odd. Is there a way to change this behaviour? It would seem logical that a query that looks for posts with a meta key not equal to something would include posts without that meta key altogether.

    
    'meta_query' => array(
                array(
                        'key' => 'hidden',
                        'value' => '1',
                        'compare' => '!='
                )
            )
        );
    
  • https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters, add a query for “NOT EXISTS”

    
    'meta_query' => array(
      'relation' => 'OR',
        array(
          'key' => 'hidden',
          'value' => '1',
          'compare' => '!='
        ),
        array(
          'key' => 'hidden',
          'compare' => 'NOT EXISTS'
        )
      );
    

    );

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘meta_query returns false if field doesn\\\'t exist’ is closed to new replies.