Skip Navigation

[Resolved] Display custom post type with date that does not exist

This thread is resolved. Here is a description of the problem and solution.

Problem:

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.

Solution:

You can create two post views, for example:

https://toolset.com/forums/topic/display-custom-post-type-with-date-that-does-not-exist/#post-1645987

Relevant Documentation:

This support ticket is created 5 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by philippeK 5 years ago.

Assisted by: Luo Yang.

Author
Posts
#1645403

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);

#1645987

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.

#1649953

My issue is resolved now. Thank you!