Posts Grouped by Years and Months in Bricks

Updated on 14 May 2024

This Pro tutorial provides the steps to display posts (can be of any post type) organized by months within years based on the published date in Bricks.

If you want to do the same but with a date-type custom field value, refer to Events grouped by years within months based on event date custom field value.

We shall

  • write a custom PHP function that prepares and returns the data needed in the form of three arrays (sample screenshot|mirror)
  • register two custom query types - one for years and another for months (in which there are published posts)
  • set up a Years query loop and within that, a Months query loop and within that, a Posts query loop to output posts that are published in the outer two loops i.e., that iteration of year and that iteration of month.

Step 1

Add the following in child theme's functions.php (w/o the opening PHP tag) or a code snippets plugin:

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