how to show posts that are missing a meta_value

Getting posts without a certain meta key is a little tricky, namely due to the database design and the nature of SQL joins.

AFAIK, the most efficient way would be to actually grab the post IDs that do have the meta key, and then exclude them from your query.

// get all post IDs that *have* 'meta_key' with a non-empty value
$posts = $wpdb->get_col( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'my_key' AND meta_value != ''" );

// get all posts *excluding* the ones we just found
query_posts( array( 'post__not_in' => $posts ) );

Leave a Comment