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
Related Posts:
- Meta query with string starting like pattern
- compare meta_query in get_posts arguments
- Getting attachments by meta value
- Order by meta_value_num DESC and meta_value ASC on WP 4.0
- WordPress altering my custom query, How to fix it?
- Querying by taxonomy vs Querying by Custom fields Speed Comparison
- Order by meta_key in custom post type doesn’t affect the query
- meta_key and meta_value not working together
- Order Posts by meta value AND published date
- How to make Meta Query case sensitive?
- Fetch Record based on meta key dates
- Meta_query with multiple keys and multiple values
- meta_query BETWEEN, but the range is stored in the custom field
- Extend product search with meta in WooCommerce
- how to make members list directory through wordpress post custom meta key.
- Large AND OR query timing out
- meta_value_num not ordering all items
- meta_compare not comparing whole integer
- Display posts with specific value first in query
- Get every post with value in meta key
- add_query_arg to compare and display events from a certain date
- Comparing 2 Decimal Numbers from custom fields and displaying posts
- meta_query weird behaviour, static int will work, but not user data
- Query multiple post types, but different order for each
- Order by the first array within a meta_query
- Query ACF relationship field – Comparator IN – Value array
- ACF: How to query for a given value count of an array like field? (e.g.: How many rows has a `flexible_content` field?)
- How to _GET multiple value checkbox WP_Query in Custom Toxonomy / Custom Fields
- How do i create a custom post query when the meta value is an array?
- WP Query – grouping posts by same meta key, adding together values from another key
- meta_query is overriding default search
- Trouble with serialized metadata
- Comparing Meta Field date in WPQuery using Meta_Query?
- Getting posts by custom field value
- Query against multiple locations within single custom post type post
- WP_Query, ACF field and array
- Order by Date Custom Field
- Query by meta value (add a dropdown of all values)
- Order Posts By Custom Field That is an array of objects
- Custom WP_query and integrating into theme file
- Using custom field as ‘orderby’ value causing post grouping by publish date
- Compare meta key separation
- query post based on comparison
- Get posts by meta value except one post [closed]
- How to query posts based on lat-lng coordinate as post meta?
- Set Alias for meta_query arguments in get_posts()
- How can I use order_by to order by two meta_keys without excluding posts that don’t have those keys initialized?
- WP_Query orderby custom field then post_date in one query
- Trying to perform complex custom field query with order by set to field value
- datetime picker, timestamps and meta queries
- Sorting Posts by custom field
- Order posts by date and then by custom field
- Compare two meta-fields in a wp_query (where meta-field-A is larger than meta-field-B)
- Meta Query querrying several post types based on ACF fields is broken after update to WP 4.1
- How to get sum of meta_values of a meta_key in wp_query according to conditions
- Difference between ‘LIKE’ and ‘IN’ in meta queries
- Order WP Query posts by custom order calculated from post meta values
- how to speed up a complex wp_query?
- How to query for custom field within Gutenberg block from outside of that post? [closed]
- Meta Query for specific months
- Filtering by Post Meta Custom Fields – Performance
- Meta query with JSON value
- Get meta info related to current post
- ACF Date Based wp_query
- exclude posts with a specific custom field and value
- Returning a list of custom post types excluding those without a specific meta_value
- Get attachment by meta_key value
- Using meta_query with multiple keys and compare values
- Mysql query and order meta value
- Get a list of posts with associated meta_value
- Is it possible to set specific posts to show first in a query?
- Order ascending is ignored in meta query?
- database query with more than a couple meta hangs and doesn’t complete
- Search posts missing a particular custom field
- Conditional operator OR not working with custom fields
- Sort by presence of thumbnail
- it’s possible make a WP_Query with math operations?
- Sorting search results with custom dropdown
- Posts query according to meta box date
- create metabox to activate slider
- Query only displays one page_id
- Display post meta conditionally based on it’s value
- What’s missing in this wp_query and meta_query
- How to use the Term Object from a custom select field in a query
- meta_query order by date present -> future then show null
- Problem with my loops
- Sort posts on custom field AND after that sort on date?
- meta_query compare > not working
- Saving custom fields for WP_Query to retrieve
- Filter posts/pages by user_role array
- WP_Query with MetaQuery issue
- WP_Query with meta_value_num and meta_query not paged correctly
- Post Query not working after updating to 4.1
- The sorting of posts by a meta_query with two keys fails while separated as single queries it works
- Sort Posts with custom meta key by default which is currently set as optional
- WP_Query with several meta_query-statements and order by meta_value
- Using ‘meta_query’ with the ‘pre_get_posts()’ hook disables searching for post titles
- Get posts using multiple values from ACF checkbox as meta query wordpress
- WP Query returning all posts when Meta_query is null
- Order custom post type by posts with most likes first