Group posts by date with GenerateBlocks

tdrayson

I recently wrote a tutorial on building a dictionary list grouped by letter. Which got me thinking. I wonder if the code could be slightly adapted to work with the post date.

This tutorial will allow you to group your posts by month and year (August 2023) or just year (2023) with the GenerateBlocks Query Loop.

Demo

1. Setup our query loop

First we need to insert our GenerateBlocks Query Loop and set a couple of parameters:

  • Order By: Date
  • Order: DESC

Feel free to style the query loop however you want. I just used the title and post date linked to the post in my tutorial, however you can add whatever information you want.

You can also add pagination to this and the solution will still work. Great for sites with a large amount of posts.

screenshot 2023 08 12 at 13.29.30

2. Adding the target class

Next we need to add the class group-by-date to the grid block inside the Query Loop Block. This is what will tell our code later on to add the date grouping functionality to the loop.

3. Add the code

Now we want to add the code which will modify our targeted Query Loop. Add the 2 code snippets below to functions.php or code snippet plugin.

Members content only

Become a snippetclub.com member to gain instant access to this tutorial plus tons more premium content.

Already a member? Log in below.

Login Form

👋🏻 Weekly Tutorial Digest

I send out a weekly newsletter with links to new tutorials written in the last week, you can subscribe below.

Newsletter

🔒I won't send you spam, I promise