Loop of years for events

Hey. I need some help. I think this one is for @aslotta, but any help will be much appreciated.

I have created a custom post type (CPT) called “events” using the Advanced Custom Fields (ACF) plugin. Within this CPT, I have a field called “event_date”.

Now, I want to create a loop that displays events organized by year. The desired structure is as follows:

  • 2024:

    • Event 1
    • Event 2
  • 2023:

    • Event 1
    • Event 2
  • 2022:

    • Event 1
    • Event 2

To achieve this, I have a function that retrieves an array of the years:

function get_event_years() {
    $years = array();
    $events = get_posts(array('post_type' => 'events', 'posts_per_page' => -1));

foreach ($events as $event) {
    $event_date = get_field('event_date', $event->ID);
    if ($event_date) {
        $year = date('Y', strtotime($event_date));
        if (!in_array($year, $years)) {
            $years[] = $year;
        }
    }
}

return implode(", ", $years);

}

Now, I need help with implementing this in a loop to get only the years. The idea is to add a second loop inside where I will query only the events in the same year.

Some help?

Hi

Actually, I have something similar and found a much simpler solution.

It works if your ACF event date field is a “Date Picker” field type:

In the ACF field settings, I made the following settings for the display format and the return value format.

The return value format is very important as this will be used in the query! I have it set as Ymd (= YearMonthDay).

For the query loop to show all events in 2024, I then used the Bricks query editor and entered this code:

return [
    'post_type'         => 'chochete_kalender',
    'posts_per_page'    => -1,
    'post_status'       => 'publish',
    'orderby'           => 'title', 
    'order'             => 'ASC',
    'meta_query'        => array(
      'relation' => 'AND',  
      array(
            'key'       => 'datum_vom_kalender',
            'value'     => '20240101',
            'type'      => 'DATE',
            'compare'   => '>='
        ),
        array(
            'key'       => 'datum_vom_kalender',
            'value'     => '20241231',
            'type'      => 'DATE',
            'compare'   => '<='
        )
    )
];

Just change the post_type and the name of the ACF field, mine is called datum_vom_kalender and adjust the from (array 1, e.g. 20240101) and the to (array 2, e.g. 20241231) values.

Now this will show all posts in the defined post types that have the ACF date value field within 01/01/2024 and 31/12/2024.

Tipp: Maybe you already know, but you can also use the dynamic data field and its custom date format directly in a title element to show the event date however you want, for example {acf_datum_vom_kalender:d.M.Y} will actually print in the front-end

Datequery2

Cheers

Patric

Hi @patrick_adversum,

Thank you for your response, but unfortunately, it is not working.

After reading your answer, I am unsure if I explained myself properly.

What I want is to have a list of the years that have events in them.

I will continue to investigate.

Ok, maybe I didn’t understand the question correctly.

Good luck

Patric

1 Like

I’m looking for a solution for this as well. I have the same problem. I have events that need to be displayed by month and year. Did you find a solution yet @marcorubiol ?

I found it here Events grouped by years within months based on event date custom field value - BricksLabs

it is 200$ to check this solution