Is there a simpler solution when you want to display element with a custom field DATE (wpcf-release-date) and that field has not always values in it. I found that if I order by this custom field then all the custom posts that have empty values for wpcf-release-date doesn't show up. So I did this trick to make it work.
I need to have all the custom posts order by their date wpcf-release-date ASC and then the empty wpcf-release-date comes after that.
Here is the code:
$meta_query_date_exist = array(
'relation' => 'AND',
'type_content' => array(
'key' => 'wpcf-type-of-content',
'value' => 'short-interview',
),
'release_date' => array(
'key' => 'wpcf-release-date',
'compare' => 'EXISTS',
)
);
$args_date_exist = array(
'post_type' => 'peace-media-library',
'orderby' => array('release_date' => 'ASC'),
'fields' => 'ids',
'posts_per_page' => '-1',
'meta_query' => $meta_query_date_exist
);
$interviews_date_exists = new WP_Query($args_date_exist);
$meta_query_date_notexist = array(
'relation' => 'AND',
'type_content' => array(
'key' => 'wpcf-type-of-content',
'value' => 'short-interview',
),
'release_date' => array(
'key' => 'wpcf-release-date',
'compare' => 'NOT EXISTS',
)
);
$args_date_notexist = array(
'post_type' => 'peace-media-library',
'orderby' => array('title' => 'ASC'),
'fields' => 'ids',
'posts_per_page' => '-1',
'meta_query' => $meta_query_date_notexist
);
$interviews_date_notexists = new WP_Query($args_date_notexist);
//now you got post IDs in $query->posts
$allTheIDs = array_merge($interviews_date_exists->posts, $interviews_date_notexists->posts);
//new query, using post__in parameter
$args = array(
'post_type' => 'peace-media-library',
'orderby' => 'post__in',
'post__in' => $allTheIDs,
'posts_per_page' => '-1',
);
$finalQuery = new WP_Query($args);
Hello,
Within Toolset Blocks/Views plugin, you can create two post views:
1) First post view :
- Query your custom posts
- Order the result by custom field "wpcf-release-date"
It should be able to output all results which have field "wpcf-release-date"
2) Second post view
- Query your custom posts
- Order by other fields instead of field "wpcf-release-date"
- Add a filter with filter hook "wpv_filter_query", find those posts do not have custom field "wpcf-release-date":
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Then display the second post view under the first view.
My issue is resolved now. Thank you!