WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I recently started using simple fields on a project to differentiate between types of posts. All posts from now on will have one of four possible meta values associated with this (Story, Article, Video or Cartoon). None of these types of posts have unique attributes or values, so I've decided not to go the custom post type route this time.

I'm aware how to query for posts with a certain meta value, but this only works on posts that already have that meta key associated with them - not for posts that were created before the simple fields plugin was installled, and put into use. I'm curious how I would query for all posts that either lack the meta key "fizzbuzz" or have the key but it's associated with a value of "foobar".

Is there a way to query for all posts that lack a key altogether?

share|improve this question

This will get you everything that doesn't have the meta key fizzbizz. The custom loop part, I lifted directly from the codex.

$pageposts = $wpdb->get_results("
    SELECT * FROM wp_posts p 
    LEFT JOIN wp_postmeta m 
    ON p.ID = m.post_id 
    WHERE m.meta_key <> 'fizzbizz'
    OR m.metakey IS NULL
    ORDER BY p.post_date DESC;

if ($pageposts):
    global $post;
    foreach ($pageposts as $post): 
// now you are in the loop, use the_title() or whatever

This will get everything where it's got the key, but the value is 'foobar'

$pageposts = $wpdb->get_results("
    SELECT * FROM wp_posts p 
    JOIN wp_postmeta m 
    ON p.ID = m.post_id 
    WHERE m.meta_key = 'fizzbizz' 
    AND m.meta_value = 'foobar' 
    ORDER BY p.post_date DESC;

and that last one is tricky, I'm not so sure on that one...


Fixed first query thanks to this question.

share|improve this answer
The second query should work, but you can do the same thing better using WP API functions. The first query doesn't look to me like it works. Looks like it will only get posts that have meta keys set (because otherwise, they wouldn't be included in the JOIN.) – goldenapples Jan 7 '11 at 7:50
@golden Good call on the 1st. I guess in order to write that one correctly, there would first need to be a way to query for posts that lack any key. Do you have any ideas on that one? – JakeParis Jan 7 '11 at 15:41
@golden fixed the first query. – JakeParis Jan 7 '11 at 17:11
Sweet! I never figured out the LEFT JOIN, so I had no idea how to do that (and believe me, I've needed to!) Now the only thing to do is figure out how to use that syntax within WP API methods. ie the second query could just be written get_posts('meta_key=fizzbizz&meta_value=foobar'); I think querying for the absence of a meta key requires special filters on 'posts_join' and 'posts_where'... anyone got a simple solution? – goldenapples Jan 7 '11 at 18:09
I think you also need a DISTINCT clause in the SELECT if you're doing it like that, otherwise you'll pull a particular post multiple times if it has multiple meta values. – goldenapples Jan 7 '11 at 18:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.