Wordpress - How to only display posts whose meta_value field is not empty?
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.
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.
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' => ''
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.
Read more about custom field parameters in the codex.