Hi- i’ve got a custom post type called ‘events’ with a datepicker field called ‘start_time’ – I’ve got the archive template ordering them by this field using pre_get_posts, however- the event in question runs over 2 days and i’m wondering if it’s possible to separate the results into the individual days?
I’ve tried several approaches with no success- it occurs to me that i need to get the query to return the contents of the start_time field’s date as an array, then loop through it as a foreach query maybe? but i’m out of ideas as to how..
i tried this which managed to get the dates and return them as text, but the fetching the template part just seems to return arbitrary events
$events = get_posts(array(
'post_type' => 'events',
'meta_key' => 'start_time',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
$group_events = array();
if( $events ) {
foreach( $events as $event ) {
$date = get_field('start_time', $event->ID, false);
$date = new DateTime($date);
$year = $date->format('Y');
$month = $date->format('F');
$day = $date->format('d');
$group_events[$year][$month][$day][] = array($event, $date);
}
}
foreach ($group_events as $yearKey => $years) {
foreach ($years as $monthKey => $months) {
foreach ($months as $dayKey => $days) {
foreach ($days as $eventKey => $events) {
echo $events[1]->format('d-m-Y');
echo '<br>';
setup_postdata($event);
get_template_part('loop-templates/content','events');
wp_reset_postdata();
echo '<br>';
}
}
}
}
Move your second foreach
block inside of the if( $events ) {
block and move the wp_reset_postdata();
statement to the last statement of the if( $events ) {
block.