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
- Meta_query compare operator explanation
- meta_query ‘compare’ => ‘IN’ not working
- Sorting: custom query with orderby meta_value_num THEN by title
- WP_Query with checkbox meta_query
- Is “orderby” in WP Meta Query conflicting with Meta Query?
- Query two post types and order by two different date fields
- WP Meta Query for some meta (array) values
- Sort users by meta_value_num
- meta_query with array as value
- Using OR in WP_Query negates the “NOT EXISTS” compare
- Using WP Query to search within ALL keys in meta query
- WP Query – Get WooCommerce Products with variation that is in stock
- Meta_query with or without value
- How can i make WP_Query return post image and categories?
- How add a custom posttype name using ACF field to a query post array
- wp_query not searching with apostrophe
- Custom query based on meta key – Reduce three states to two in results?
- Add quicklink to in the Admin posts page where I can query by a meta_key
- Performance when getting post meta for post retrieved by meta value
- How to display multiple custom fields with the same meta_key in an ascending order?
- Order by empty custom field
- Tax query AND/OR meta query [duplicate]
- Help ordering Post loop by two meta values
- Query post by date (stored custom field meta as yyyymmdd) and differentiate across 12 months
- WP Query Meta Value – How To Identify Specific, Unique Values?
- Order by meta_key doesn’t work
- WP_QUERY with group by with custom fields
- Intiger meta value with ‘>=’ returns posts with lower value
- Multiple meta_key ordering with pre_get_posts
- Query using string from custom field as array value
- How to show list of posts with custom field value (a date) that are coming soon
- Meta query with compare by more than 2 fields
- WP_Query meta compare must include ALL array values
- meta_query returning excluded result
- Woocommerce Shortcode which displays a table with product data
- Creating a query that get all posts but places meta items first
- get_posts query is taking about 40 seconds to execute
- wp query to use both author id and meta_query
- WP_Query for a taxonomy value OR a custom post type meta field
- Meta query orderby meta_value_num sorting by role first
- Query post by Category and custom file (ACF)
- Sort custom post archives by a meta value from a different custom post type?
- meta_query is not allowing to show posts when querying multiple arguments in a WP_Query
- Speed up WP_query with meta comparing dates
- Meta Query if Values Don’t Exist
- How to show all the associated posts with specific date of data metabox?
- Is it possible to search for a string in posts OR postmeta?
- Custom search (wp query by custom fields)
- Selecting posts older than the current Unix epoch timestamp
- Custom Search Query – include only custom fields and title
- Why is WP_Query’s meta_query not filtering results?
- meta_query in wp_query not working as expected
- WP_Query – order with usort by custom meta
- WP query with multiple custom meta not respecting orderby
- Passing a variable containing a comma separated list of values in a meta-query