This can be done by providing a “default” value to the ORDER BY
statement.
Try adding this before your call new WP_Query($args);
:
$setDefaultOrderby = function($statement) {
return str_replace('wp_postmeta.meta_value', "COALESCE(wp_postmeta.meta_value, 'false')", $statement);
};
add_filter('posts_orderby', $setDefaultOrderby);
and this after it:
remove_filter('posts_orderby', $setDefaultOrderby);
Change the 'false'
with whatever the default value is that you need.
I am using COALESCE
here, which basically selects a first argument you pass to it that is not NULL
, essentially allowing you to provide a fallback for a post that has no meta key.
Related Posts:
- ACF Relationship Field Search Filtering [closed]
- ACF datepicker meta_query Compare Dates in m/d/Y g:i a – Not in Ymd Format
- meta_query with array as value
- If two first numbers exist in wp_meta_query value
- WP_Query meta_query >= date
- ACF Date Based wp_query
- What is the random string I am seeing when I use get_query_var?
- Any number in meta key (wp query)
- WP_Query with meta_query dosen’t return results
- Custom query filter by ACF date custom field
- Order WP_Query by meta_key priority when ‘OR’ relation used for multiple meta values
- ACF: How to query for a given value count of an array like field? (e.g.: How many rows has a `flexible_content` field?)
- WP_Query, ACF field and array
- Pre get posts sort by meta key returns no results if meta key does not exist
- Slow wp_posts and wp_postmeta query using Advance Custom Fields
- Get posts meta_query by repater field
- How can I modify standard search query to include also ACF custom fields values?
- The sorting of posts by a meta_query with two keys fails while separated as single queries it works
- Get posts using multiple values from ACF checkbox as meta query wordpress
- How do I subquery with custom meta fields?
- Use value from meta key array for use in WP_Query
- Meta_query compare operator explanation
- Nested meta_query with multiple relation keys
- meta_query ‘compare’ => ‘IN’ not working
- Sorting: custom query with orderby meta_value_num THEN by title
- Using OR conditions in meta_query for query_posts argument
- WP_Query with checkbox meta_query
- Query Multiple Post types each with own meta query
- 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?
- WordPress meta_query and order by custom field
- How can I create a WP_Query that returns posts where one meta_value
- Sort users by meta_value_num
- 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
- Query Posts depends on custom field inside repeater field using acf
- Add condition of user capability in WP_query
- Performance when getting post meta for post retrieved by meta value
- Adding an array from a query string to a WP meta_query
- Query multiple meta values
- How to display multiple custom fields with the same meta_key in an ascending order?
- Meta_query with multiple keys and multiple values
- How to Union two different conditions in one WP_Query
- Tax query AND/OR meta query [duplicate]
- Excluding pages in WP_query using ACF
- Help ordering Post loop by two meta values
- How to do a wp_query with two acf-fields, sorting on one of them
- Using Advanced Custom Field for file upload, how do I load the file url
- Multisite wp_query & switch_to_blog issue
- meta_compare not comparing whole integer
- Is this meta query problematic?
- JSON – Create rest api endpoint for Advanced Custom Fields
- Selecting posts with a given meta value for a meta key
- Appending to existing WP_Query’s meta_query if exists
- Get meta_value of a specific meta_key from all posts belonging to a specific custom type
- Is it possible to retrieve posts depending on meta_key LIKE condition? [duplicate]
- Nested array issue in meta_query
- Error in meta_query not get result
- Comparing 2 Decimal Numbers from custom fields and displaying posts
- How to get current page nearest parent id?
- meta_query with array as value with multiple arrays
- Why doesn’t my WP Meta Query return any results?
- Optimising WP_Query with ACF Fields
- WP_Query – display posts by custom field and order by another
- Order (by ASC) posts with meta_key so posts without values are last
- Search filter between promo and exact price
- Populate select list with meta values from all posts of a Custom Post Type
- Form checkbox value going to dynamic URL
- meta_query – check for multiple meta values in key which holds an array of values
- WordPress Meta Query Null Values Order
- show most viewed post
- Searching for a specific month in a metadata saved as Timestamp (Wp_Query)
- Why can my filter query SOME metadata but not other metadata?
- Custom query showing all acf field values instead of the one searched
- WP_Query on custom key and value
- Get posts having meta value between two numbers
- How to set meta_query if get_post_meta returns nested array for that key? [duplicate]
- Sorting with meta_query and multiple, optional meta keys
- meta_query with relation = AND not working as expected when combining EQUALS with NOT EQUALS
- WP Query for Multiple Post Types
- Query by meta value (add a dropdown of all values)
- ACF: using two loops, the_field returns field content from another loop
- Orderby query does not work for custom fields even with meta query
- make query more simplest and in one query
- WP_Query returns empty if meta_query has more than 7 values
- Compare meta key separation
- WordPress meta_query not working
- Using orderby with 2 meta keys
- Passing conditional arrays to WP_Query() [closed]
- Query posts filter not working
- Creating array to compare custom field values
- WordPress Query Default Order if Meta Values Same?
- Display all custom meta field values from the database using ACF Repeater
- Wp query by 2 meta field – check if they exists / have value
- Get all user with both meta_value
- How to get all post_id and meta_value using meta_key in wp_postmeta table
- WP Query – Show custom posts only if user contain some user meta
- Showing Counts on Comment List with Custom Query