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
- How to use meta_query to retrieve posts from multiple custom post type
- WP_Query filtering in ACF field containing dates
- Meta query with string starting like pattern
- compare meta_query in get_posts arguments
- how to show posts that are missing a meta_value
- Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
- WP_Query using meta_query with relation OR and orderby meta_value doesn’t work
- querying with custom meta field with meta_query
- Order by two meta keys
- multiple meta key but get server load is very high
- A WP_Query that will look for posts after 2 weeks ago OR with a certain 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?
- Order by meta_key in custom post type doesn’t affect the query
- Use meta_query to display events by date in custom field
- Minimising number of queries on a page when using Advanced Custom Fields
- How to show recent and upcoming birthdays using meta_query
- Order Posts by meta value AND published date
- ACF Relationship + WP Template Parts
- Using WP_Query and WP_Meta_Query Outside of WordPress
- WP_Query on custom field and order results
- Tax query clause inside a meta query clause?
- How can I display a custom field from all posts and order them individually from their parent post
- Fetch Record based on meta key dates
- meta_query in WP_Query value is not accepting array
- Stuck in Order by more then one
- Is there a way to work with a specific the_field from an already queried post? [closed]
- Changing sort order for presentation by Jetpack infinite scroll
- meta_query not working as expected
- get posts by meta_key from inside key of post meta
- Help optimizing slow WP_Query with meta_query
- meta_value_num not ordering all items
- Meta Queries – should nesting work after WP 4.1?
- Filter and Order by Multiple Custom Meta Values
- Meta Query relation “AND” then set array accordingly
- 2 weeks ago from meta query meta value
- meta_compare not comparing whole integer
- Include post content of linked posts in search
- meta queries do ‘either/or’ checks
- How to make posts unqueryable/unpublish posts where ACF relationship field is an unpublished post? [closed]
- WP Query ordering on a meta field for posts with a date in the future not ordering by datetime
- Merge 2 custom post type posts and taxonomy terms and sort ascending
- Selecting posts with a given meta value for a meta key
- Get every post with value in meta key
- get_page meta query not working, maybe a bug?
- Query by date from custom field
- Is it possible to retrieve posts depending on meta_key LIKE condition? [duplicate]
- Comparing 2 Decimal Numbers from custom fields and displaying posts
- Using WP meta query to show custom post types by a start and finish date
- Sort posts using multiple custom fields and menu_order in single query?
- 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
- WordPress Meta Query Null Values Order
- show most viewed post
- Why can my filter query SOME metadata but not other metadata?
- Getting posts by custom field value
- How to get taxonomy image attached to a Post Type WordPress
- Custom query showing all acf field values instead of the one searched
- wordpress multiple meta value query is not working
- Strange results from WP_Query
- meta_query with relation = AND not working as expected when combining EQUALS with NOT EQUALS
- WP Query for Multiple Post Types
- Order Posts By Custom Field That is an array of objects
- Orderby query does not work for custom fields even with meta query
- make query more simplest and in one query
- Use not custom fields in get_posts() meta_query?
- Compare meta key separation
- Query posts filter not working
- Creating array to compare custom field values
- WordPress Query Default Order if Meta Values Same?
- Custom query (author is post_author or meta co_author) with Pagenavi pagination
- Wp query by 2 meta field – check if they exists / have value
- Using ‘meta_query’ with the ‘pre_get_posts()’ hook disables searching for post titles
- WP query with multiple custom meta not respecting orderby
- Passing a variable containing a comma separated list of values in a meta-query
- Complicated `orderby` based on text in custom field
- meta_query post_date not returning results