Leave a Reply

10 comments

  1. Hi @Rob:

    The reason you can’t figure out how to do it is because it’s not possible, at least not without resorting to SQL. Try adding the following to your theme’s functions.php file:

    add_filter('posts_where','yoursite_posts_where',10,2);
    function yoursite_posts_where($where,$query) {
      global $wpdb;
      $new_where = " TRIM(IFNULL({$wpdb->postmeta}.meta_value,''))<>'' ";
      if (empty($where))
        $where = $new_where;
      else
        $where = "{$where} AND {$new_where}";
      return $where;
    }
    

    If you have custom 'featured_image' fields with empty values the above will filter them out. If you problem is something else, we’ll have to see what your data looks like to solve it.

    One thing I’m curious about; how did you get empty values for 'featured_image'? The admin UI in WordPress 3.1 does its best to keep you from entering empty values.
    Hope this helps.

  2. This seems to work for getting the value into the query, not sure about whether it pulls valid results though..

    'meta_query' => array(
        array(
            'key' => 'some_key',
            'value'   => array(''),
            'compare' => 'NOT IN'
        )
    )
    

    Not had time to create fields to test the results, but i’ve been watching queries i’ve worked with today and noticed NOT IN will happily take an empty array.

  3. This is an old question, but it seems WordPress has fixed this “missing feature”: now, according to WordPress Codex is possible to check for existence (or non-existence) of the meta key, like this

    'meta_query' => array(
        array(
            'key' => 'featured_image',
            'compare' => 'EXISTS', //or "NOT EXISTS", for non-existance of this key
        )
    )
    

    This is available as of WP >= 3.5.

  4. If you want the meta value to exist and have some value other than an empty string:

          'meta_query' => [
            'relation' => 'AND',
            [
              'key' => 'some_key',
              'compare' => 'EXISTS',
            ],
            [
              'key' => 'some_key',
              'compare' => '!=',
              'value' => ''
            ]
          ]
    
  5. This is the query that worked for me. Very similar to the comparison in t31os’s answer from 2011, but because the meta key/value is just a simple textstring, it doesn’t need to be a meta_query array.

    $args = array(
        'posts_per_page' => 5,//replaced 'showposts' in version 2.1
        'meta_key' => 'featured_image',
        'meta_value' => array(''),
        'meta_compare' => 'NOT IN'
    );
    

    For whatever reason, using 'meta_value' => '' (set to an empty string) and 'meta_compare' => '!=' or 'meta_compare' => 'NOT LIKE' still pulled all posts for me, but it probably has something to do with the fact that I created my meta value using the Advanced Custom Fields (ACF) plugin.

    While using EXISTS may work, it does not check if the value is empty or not, so it’d still pull posts that have the meta field even if the meta value is blank or empty.

    Read more about custom field parameters in the codex. Possible values for meta_compare:

    • =
    • !=
    • > – works with numeric, DATE, and DATETIME meta value types
    • >= – works with numeric, DATE, and DATETIME meta value types
    • < – works with numeric, DATE, and DATETIME meta value types
    • <= – works with numeric, DATE, and DATETIME meta value types
    • LIKE – can work with an array of meta values
    • NOT LIKE – can work with an array of meta values
    • IN – meta value should be an array of values
    • NOT IN – meta value should be an array of values
    • BETWEEN – works with numeric, DATE, and DATETIME meta value types as well as an array of values
    • NOT BETWEEN – works with numeric, DATE, and DATETIME meta value types as well as an array of values
    • EXISTS – no need to specify a meta value for WP versions 3.9 and above
    • NOT EXISTS – no need to specify a meta value for WP versions 3.9 and above
    • REGEXP
    • NOT REGEXP
    • RLIKE
  6. Am i missing something?

    <?php 
        $args = array(
            'post_type' => 'post',
            'posts_per_page' => -1,
            'meta_key' => "featured_image"
        );
        $the_query = new WP_Query( $args ); 
    
    ?>
    

    Won’t that do it?

  7. Hopefully this may help. You could always use the ‘EMPTY’ or ‘NOT EMPTY’ comparator like so:

           'meta_query' => [
            'relation' => 'AND',
            [
              'key' => 'your_key',
              'compare' => 'NOT EMPTY',
            ],
         
          ]
    

    Use this with one of my checkbox ACF fields which seems to work really well. In my case, I was doing the opposite of yourself and some of the posts I was querying didn’t have an entry for this meta field so I simply added the NOT EXISTS option as a second comparison.

    Querying posts that either did not contain the meta field or contained an empty field:

     'meta_query' => [
            'relation' => 'AND',
            [
              'key' => 'your_key',
              'compare' => 'NOT EXIST',
            ],
            [
              'key' => 'your_key',
              'compare' => 'EMPTY',
            ],
         
          ]
    
  8. So I had this problem specifically when using ACF & when using a field that held a multiple select that had previously had been populated and now was empty.

    I had the issue that:

        'relation' => 'AND',
        array(
           'key' => 'field_name',
           'compare' => 'EXISTS'
        ),    
        array(
          'key' => 'field_name',
          'value' => '',
          'compare' => '!='
        ),
    

    Was returning all the posts whether the meta was empty or not?

    So instead I looked to see if the field contained a double quote:

        'key' => 'field_name',
        'value' => '"',
        'compare' => 'LIKE'
    

    As ACF uses double quotes in an array structure for multi selects stored in the DB.

    Hope this helps someone who has fallen into the same trap as I 🙂