Leave a Reply

1 comment

  1. 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): 
        setup_postdata($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…

    Edit

    Fixed first query thanks to this question.