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 get products with the same custom attribute like ean code
- Order by multiple custom fields within woocommerce hook for catalog ordering (woocommerce_get_catalog_ordering_args)
- Meta_query compare operator explanation
- meta_query with meta values as serialize arrays
- Nested meta_query with multiple relation keys
- Can wp_query return posts meta in a single request?
- meta_query ‘compare’ => ‘IN’ not working
- Use REGEXP in WP_Query meta_query key
- WP Query Args – Title or Meta Value
- How to query posts based on lat-lng coordinate as post meta?
- how to show posts that are missing a meta_value
- 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?
- Sorting: custom query with orderby meta_value_num THEN by title
- Display products from specific category in shop page
- Can I query custom meta data through WP_Query
- Function in array as arguments for WP_Query
- How to Compare Two Meta Fields
- Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
- Meta Query with AND & OR?
- WP_Query to show post from a category OR custom field
- Does tax_query really beats meta_query in all situations?
- WP_Query with checkbox meta_query
- WP_Query orderby custom field then post_date in one query
- ACF Relationship Field Search Filtering [closed]
- Add indexing to meta_value in wp_postmeta
- Is it possible to orderby multiple meta_keys when using meta_value_num?
- Custom WP_Query order by post_meta and (author) user_meta
- Sorting posts by custom date fields (non standard date format)
- Query WooCommerce orders where meta data does not exist
- Filtering a WP_Query meta_query by numeric values isn’t working
- Query Multiple Post types each with own meta query
- Execute a large WP_Query with many “AND” Meta_Queries?
- Query all posts where meta value is empty
- meta_query where value is equal to given value
- WP_Query min and max values
- ACF datepicker meta_query Compare Dates in m/d/Y g:i a – Not in Ymd Format
- WP_Query using meta_query with relation OR and orderby meta_value doesn’t work
- Using custom meta_query with relation not working as expected
- Get_post() with meta_key when compare is a date
- Perform query with meta_value date
- Order by two meta keys
- multiple meta key but get server load is very high
- Meta Query with date and time on the same Day before given time
- A WP_Query that will look for posts after 2 weeks ago OR with a certain meta value
- How to grab metabox value in wp_query meta_query key
- Group posts by custom field
- WP_Query meta_query where meta value ends in space
- Trying to perform complex custom field query with order by set to field value
- Finding all results from database within 500 miles of the given latitude and longitude [closed]
- Nested query inside Logical operator OR not working in meta_query
- Is “orderby” in WP Meta Query conflicting with Meta Query?
- Query by meta_key and order by meta_value_num return orderby date
- What is an efficient way to query based on post_meta?
- WP_Query not working as expected for attachments and custom meta_query
- datetime picker, timestamps and meta queries