Events Grouped by Event Year

In this Pro tutorial, we share the PHP code for outputting past events grouped by the event year in descending order of the event year.

Scenario:

CPT: event
Date custom field: event_date

Sample events:

Current date at the time of writing this tutorial: 5 Apr 2023

Output:

We shall

  • get all the past events using get_posts() and a meta_query
  • loop through the past events and store them as values of an array whose keys are the years
  • sort years in descending order
  • loop through the years and output the events grouped by years

Note: This code can be used in any WordPress site and is not Bricks-specific.

PHP

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Instant access to 390+ Bricks code tutorials with BricksLabs Pro