Getting posts without a certain meta key is a little tricky, namely due to the database design and the nature of SQL joins.
AFAIK, the most efficient way would be to actually grab the post IDs that do have the meta key, and then exclude them from your query.
// get all post IDs that *have* 'meta_key' with a non-empty value
$posts = $wpdb->get_col( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'my_key' AND meta_value != ''" );
// get all posts *excluding* the ones we just found
query_posts( array( 'post__not_in' => $posts ) );
Related Posts:
- 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
- ACF Relationship Field Search Filtering [closed]
- Query Multiple Post types each with own meta query
- ACF datepicker meta_query Compare Dates in m/d/Y g:i a – Not in Ymd Format
- Meta Query with date and time on the same Day before given time
- How to grab metabox value in wp_query meta_query key
- Is “orderby” in WP Meta Query conflicting with Meta Query?
- How can I create a WP_Query that returns posts where one meta_value
- WP Meta Query for some meta (array) values
- Sort users by meta_value_num
- meta_query with array as value
- How to count data records in wordpress which have same meta_value in wp_postmeta table?
- 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
- wp_query not searching with apostrophe
- Add condition of user capability in WP_query
- Custom query based on meta key – Reduce three states to two in results?
- How to make Meta Query case sensitive?
- Performance when getting post meta for post retrieved by meta value
- Any number in meta key (wp query)
- How to display multiple custom fields with the same meta_key in an ascending order?
- Meta_query with multiple keys and multiple values
- Is it a good idea to improve meta query performance by adding tax query?
- Modify wp-query and sort by post meta key
- Tax query AND/OR meta query [duplicate]
- Orderby listing issue
- Help ordering Post loop by two meta values
- Query posts with “non set” meta value
- Query with meta_query and tax_query together not working properly
- Array as ‘key’ in WP_Query
- Ordering a mysql style datetime stamp with meta_query
- Is it possible to query a custom field where the value is between two fields?
- Meta Query doesn’t works as espected
- Filter posts by comparing custom meta value against postdate
- Wp_query with 2 meta keys and array of meta values
- meta_query: check if number exists
- Searching for meta_key returns 0 posts
- Event with multiple dates, display events chronologically
- meta_query fails to compare on values containing apostrophes
- Check if searched number is within the post meta value
- add_query_arg to compare and display events from a certain date
- WP Query / Meta Query [duplicate]
- Query posts by searching for a string in a meta field
- WP_Query not returning correct result with meta_query parameter
- Query multiple post types, but different order for each
- How to add date_query to meta_query array
- Order by the first array within a meta_query
- Query ACF relationship field – Comparator IN – Value array
- Query on meta values and post title
- Combine query in WP_User_Query()
- Multiple nested meta queries
- 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?
- How can I modify the query by adding to the existing query?
- Custom Meta Query doesn’t work the same on two site
- WP Query – grouping posts by same meta key, adding together values from another key
- Meta query ignores multiple values of the key
- How to do WP_User_Query for Nested Array value Inside Nested Array field
- Query for current post
- WordPress Query custom ordering by temporary variable
- Custom query to retrieve oldest post and retrieve others with date interval
- Comparing Meta Field date in WPQuery using Meta_Query?
- How to create better WP_Query to look for date time which is anywhere between two meta values?
- Very slow query generated getting meta data from posts
- WP_Query, ACF field and array
- How to filter on placeholder image
- Filtering WP_Query based on wp_postmeta keys values
- Complex Meta Query
- Slow wp_posts and wp_postmeta query using Advance Custom Fields
- Multiple meta_query not working
- compare for multiple meta values for same key in wp_query
- datetime picker, timestamps and meta queries
- WP_Query with meta_query won’t orderby
- meta_query only check if both value are set
- Query 3 meta_key and orderby
- wp_query sorting – one specific meta_key value at last and then sort by create date
- What is the best way to reset a search on a meta_key / meta_value?
- Slow query when selecting with large meta query or post__in
- WP Meta Query at depth 2
- Extending WP Query: Custom geolocation meta values work, but tax_query breaks
- Using custom field as ‘orderby’ value causing post grouping by publish date
- Is there any difference between below WP_Query code snippets?
- Get closest event where meta_key field is an array
- Set right order for query that returns posts based on two custom fields
- Sorting: custom query with orderby meta_value_num THEN by title
- Meta_query weird behaviour
- meta_query compare not doing what I want it to do
- Looking for a way to exclude frontpage and nav menu from query filter
- Creating Custom Query
- Can’t figure out query logic
- WP_Query orderby not working on custom post_type
- Get posts by meta value except one post [closed]
- WP_Query by meta key not returning any posts
- WordPress extremely slow when using get_posts with multiple meta_query relations
- Showing Counts on Comment List with Custom Query
- Use value from meta key array for use in WP_Query