querying with custom meta field with meta_query

There’s actually a better solution that will (hopefully) be rolling out in WordPress 3.4 — you can run the patch as a hotfix now if you’d like, but here’s the TRAC link for the patch:

http://core.trac.wordpress.org/ticket/18158

With this, you can do …

    $my_query = new WP_Query( 
        array( 
            'meta_query' => array( 
                array(
                    'key' => 'foo',
                    'compare' => 'NOT EXISTS'
                )
            ) 
        ) 
    );

or, swap it out for ‘compare’ => ‘EXISTS’ instead if you like.

-George

Leave a Comment