0

First of all I use ACF and Timber (TWIG).

I'm trying to create a calendar with a classification by year, month and hour.

I created a custom ACF field : event_date and an another field for start time event_hour_start

All works fine with years and months but how can I order posts by hour for each month please ?

2020

  • November
    • November 1 - 14:00
    • November 16 - 14:00
    • November 16 - 16:00
  • December
    • December 12 - 11:00
    • December 18 - 12:00
    • December 18 - 16:00

tpl_agenda.php

$events = Timber::get_posts(array(
    'post_type' => 'events',
    'meta_key'  => 'event_date',
    'orderby'   => 'meta_value_num',
    'order'     => 'DESC'
));

$events_list = array();

if($events) {
    foreach($events as $event) {
        $date = get_field('event_date', $event->ID, false);
        $hour_start = get_field('event_hour_start', $event->ID, false);
        $hour_end = get_field('event_hour_end', $event->ID, false);

        $date = new DateTime($date);

        $year = $date->format('Y');
        $month = $date->format('F');

        $events_list[$year][$month][$hour_start] = array($event, $date, $hour_start, $hour_end);
    }
}

$context['events_list'] = $events_list;

tpl_agenda.twig

{% for year, years in events_list %}
    <p>Year : {{ year }}</p><br>
    {% for month, months in years %}
        <p>Month : {{ month }}</p>
        {% for event, events in months %}
            <p>{{ events[0].post_title }}</p><br>
        {% endfor %}
    {% endfor %}
{% endfor %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.