How to exlude posts that have certain meta_value?

I want to exclude some posts from the home page. So I want to use meta data, and filter all posts that are signed by meta_value=0.
Like this:

$args = array(
'posts_per_page'=>28,
'meta_query' => array(
array(
    'key' => 'show_on_home',
    'value' => '0',
    'compare' => 'NOT LIKE'
    )
)                   
);
$query = new WP_Query( $args );                         

So, if I sign some post with meta_key=0 it will not apear.
The problem is that I have a lot off posts that don’t have meta data at all, and I don’t want to filter them.

1 Answer
1

WordPress 3.5 and up supports EXISTS and NOT EXISTS comparison operators.

compare (string) – Operator to test. Possible values are ‘=’, ‘!=’,
‘>’, ‘>=’, ‘<‘, ‘<=’, ‘LIKE’, ‘NOT LIKE’, ‘IN’, ‘NOT IN’, ‘BETWEEN’,
‘NOT BETWEEN’, ‘EXISTS’ (only in WP >= 3.5), and ‘NOT EXISTS’ (also
only in WP >= 3.5). Default value is ‘=’.

http://codex.wordpress.org/Class_Reference/WP_Query

So…

array(
    'key' => 'show_on_home',
    'compare' => 'NOT EXISTS'
)

… should show all posts that don’t have that key. I think that is what you are trying to do.

Leave a Comment