You can accomplish this with multiple meta queries.
$query->set( 'meta_query', [
'relation' => 'OR',
'wpcf-start-date' => [
'key' => 'wpcf-start-date',
'compare' => 'EXISTS',
],
'no-start-date' => [
'key' => 'wpcf-start-date',
'compare' => 'NOT EXISTS'
],
] );
$query->set( 'orderby', 'wpcf-start-date' );
$query->set( 'order', 'ASC' );
This will tell WP to create a query that will match every post, whether or not is has the key, setting it up as a LEFT JOIN, and then sorts on the value of that key, including those with NULL values.
Related Posts:
- WordPress custom search form with pre_get_posts not work
- Searching through different categories on different pages code is not working
- Including only current user’s posts in search
- using pre_get_posts for search results not found
- Sorting search results with custom dropdown
- $wp_query->found_posts not returning correct value
- Block internal search queries with pre_get_posts and regex rules
- Search Exact on WordPress – Relevannsi Solution Not Working
- Excluding posts from search results page with meta query not working
- Limit default Search query to post_title
- Search query alteration not working for meta values
- Can I force WP_Query to return no results?
- WP_Query orderby post__in remains ineffective in the Loop [closed]
- How-to exclude terms from the main query the most performant way?
- Want to redirect if search query match exact title of any post
- Slow SQL_CALC_FOUND_ROWS Query
- How to support multiple search terms query within one process?
- Custom search form with empty parameters
- How Do I Use WP_Query to Run This Database Query as Search Result?
- Sorting Posts by custom field
- Recommended way to drop a pending query (in pre_get_posts)?
- Search – Ajax – Alter Query Parameters with Pagination
- Multiple search queries on one page
- How to sort posts in admin by titles with dd.mm.yyyy format?
- tax_query shows no results if nothing is selected
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- How to Modify Taxonomy Archive Page with Search Parameter?
- How to display future posts – modified query still yields 404
- Custom category search box for WordPress
- Refine search results using WP_Query
- Search Posts with Custom Fields as query
- Assigning a meta_query value based on regexp to wp_query fails
- Store sticky post’s ids in a transient
- Show Sticky Post at the top but do not show again in the loop?
- Using meta_query with multiple keys and compare values
- Can I set my WP_Query to be a Main Query?
- Remove category from query (show all posts in archive.php) pre_get_posts()
- Show scheduled posts in main loop but not in WP_Query?
- wp_query ‘s’, search filter with pagination is not working
- Display filtered results into custom page
- hide custom post types with specific meta key ON admin backend
- WordPress pagination not working on template search.php
- Cutom wp_query for geolocation search, pagination not working
- Search Query for multiple categories using ‘OR’ but having certain categories be ‘AND’
- query_vars overriding WP_Query args
- how can I override The Events Calendar query modifications?
- Help ordering Post loop by two meta values
- Order ascending is ignored in meta query?
- How to make search for posts using get method?
- How Can I Change The Tax Query For The Main Loop For Taxonomy Archives?
- Search posts missing a particular custom field
- How do I get my custom query to work with search results after the first page?
- pre_get_posts – Trying to get property of non-object warning
- Replacing search results with custom external query
- Prevent author bio page from showing in search results
- Get a list of posts by specific category
- WordPress Custom Search Form Displaying Unexpected Results
- queried_object using pre_get_posts gets notices and warning
- date_query in pre_get_posts out of memory
- Slow Query On Search
- Multiple meta_key ordering with pre_get_posts
- Child pages not affected by orderby
- Query by key or author
- How to apply pre_get_posts to a custom query?
- Searching not only by post name but also category
- WP_Query, Paginate by Custom Field
- is_user_logged_in not working from WP-API (wp-json)
- How to make the WP query search for the “s” arg in other places too
- I have problems with the search query using multiple post types
- pre_user_query vs pre_get_posts
- Can I alter the main loop to ‘orderby’ a custom callback?
- How can I re-query post_type and rewrite the url?
- How to extend WP_query to a specific table?
- Display Search Results by tag_ID in my search.php
- getting posts by tags
- Add to search posts query array with post IDS which will appear first
- Custom Post Type WP_Query with filters and search
- How order by works?
- Get posts by similar names and categories
- Logging search queries for logged-in users
- How to show terms from another taxonomy
- Search Field that allows options of “Match any keyword” or “Match all keywords”
- A very strange problem with search query
- Modify query to exclude certain ids and certain parents and its corresponding children
- Search format not matching taxonomy query
- Search Functionality broken by the wp 4.2 update
- wp_query – Modify $query to include duplicate content
- Custom search (wp query by custom fields)
- pre_get_posts or $where, which one to use?
- Filter WP_Query output before it is accessed (pre_get_posts)?
- Custom Search Query – include only custom fields and title
- $wp_query when new WP_Query
- Search Query that Includes Custom Table
- Sort posts in dashboard using custom field; also include posts where field isn’t set
- Avoiding page loop
- Custom post types loop on a page template
- Why does WP_Query not search for two ‘meta_query’ keys separated with OR?
- Pre_get_post on CPT archive page
- Using ‘meta_query’ with the ‘pre_get_posts()’ hook disables searching for post titles
- How does Show Posts based on a keyword search work in WP_Query