Don't leave! This isn't another question about how to orderby meta_value!
I have a post type called
events, and each post I create has a meta field I imaginatively called
event_dates_wp which accepts multiple dates and saves them as an array in
I have a
WP_Query which returns posts with a date between the two
values, like so:
$args = [ 'post_type' => 'event', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'event_dates_wp', 'posts_per_page' => -1, 'meta_query' => [ [ 'key' => 'event_dates_wp', 'type' => 'DATE', 'value' => '20180801', 'compare' => '>=' ], [ 'key' => 'event_dates_wp', 'type' => 'DATE', 'value' => '20180831', 'compare' => '<=' ] ] ];
This all works wonderfully, and returns a list of posts which contain a
event_dates_wp value which falls between 1st of August 2018 and 31st of August 2018; but the results are 'post centric' rather than 'date centric' - let me explain myself:
The above is what one would expect, but what I want to do is list the dates, starting with the 1st of August, all the way through to the 31st, with each matching post listed under each date, allowing them to repeat.
Event A contains dates [20180801, 20180802, 20180803, 20180804] Event B contains dates [20180803, 20180804, 20180805] Event C contains dates [20180804, 20180807]
The results would look something like:
20180801 // 1st Aug 2018 Event A 20180802 // 2nd Aug 2018 Event A 20180803 // 3rd Aug 2018 Event A Event B 20180804 // 4th Aug 2018 Event A Event B Event C 20180805 // 5th Aug 2018 Event B 20180806 // 6th Aug 2018 [NO RESULT] 20180807 // 7th Aug 2018 Event C 20180808 // 8th Aug 2018 [NO RESULT] 20180809 // 9th Aug 2018 [NO RESULT] ...
The only way I can think to do this would be to query a single date at a time, then moving on to query the next; which can't be a good idea. Is there a way to achieve this seemingly common pattern of querying date based events and formatting them as described?
A good example of this kind of behaviour is the Barbican events page, which I believe is using Drupal. There surely must be a WordPress way to do this kind of thing?
Thanks for reading!