Events grouped by event years



CPT: event
custom field created using ACF: event_date


Requirement: Display events grouped by event years.

Non-working (the latest event is shown for each iteration) starter attempt:

<Loop items=2019,2022,2023,2024>
  <h3><Field name="{Get index}" /></h3>

    <Loop type=event custom_field=event_date custom_field_value="{Get index}">
      <li><a href="{Field url}"><Field title /></a></li>
  1. How to replace the hard-coded years string in the 1st line with the result of a function call?

  2. Atm, the check is being done to see if there are any event posts whose event_date is matching the provided list items (like 2019, 2022) one by one. How to do the equivalent of

$event_date = get_post_meta( get_the_ID(), 'event_date', true );
$year = wp_date( 'Y', strtotime( $event_date ) );

and check that against the list item in the current iteration?

I understand that it is possible to use calendar years in the outer loop. But I need to use the years from the custom field values.

Where did you find this syntax? The way to display items in a list loop is with <Field /> without specifying a field name. This also means that your inner loop should be custom_field_value="{Field}".

I think you might simply be thinking about this the wrong way, which makes sense. You want to only display years if there’s an event within that year. But that doesn’t necessarily mean that the data related to your years needs to come from the custom field itself. Instead, you could loop through all years and then only display the year if there are posts that match that year. Same end result on the front end, but no need to build a list of specific years using data from some fields.

The way I’d achieve this would be this general structure (note that I haven’t tested this I’m just illustrating my idea as to how I’d structure this):

<Loop type=calendar_year from=2019>
  <If loop exists type=event custom_field=event_date custom_field_value="{Field year}">
    <Field year />
        <li><a href="{Field url}"><Field title /></a></li>