# Grouping Posts by Year and Month

**URL:** <https://forum.pinegrow.com/t/grouping-posts-by-year-and-month/4718>\
**Category:** Pinegrow WP\
**Created:** [November 25, 2020, 6:16pm UTC](https://forum.pinegrow.com/t/grouping-posts-by-year-and-month/4718 "2020-11-25T18:16:32Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![RobM](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@RobM](https://forum.pinegrow.com/u/RobM)\
**Post date:** [November 26, 2020, 2:19pm UTC](https://forum.pinegrow.com/t/grouping-posts-by-year-and-month/4718/2 "2020-11-26T14:19:54Z")

</div>

@HoWin I’m still learning a lot about WordPress in Pinegrow, but here is how I did it.

### For the year

1. Place an `<h1>` where you want your Year to appear on the page
2. Select the element and add an If Option from the WordPress Actions
3. For the condition enter `$old_year !== $current_year`
4. Next add a PHP Code block from the WP Actions
5. For the function add `echo $current_year` and replace the Content
6. Add a PHP Code 2 block from the WP Actions
7. For the function add `$old_year = $current_year` and for replace select Insert after  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/4/43e9dbde30c2d7da72392d0d31107326ff2d5ed9.png)

### For the month

1. Add an `<h2>` element where you want the month to appear
2. Select the element and add an If Option from the WP Actions
3. For the condition enter `$old_month !== $current_month`
4. Next add a PHP Code block from the WP Actions
5. For the function add `echo $current_month` and replace the Content
6. Add a PHP Code 2 block from the WP Actions
7. For the function add `$old_month = $current_month` and for replace select Insert after  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/e/e044f8dfc9e7733414682752ea497141c4a51230.png)

### Set-up the variables

I added my variable initiation to the loop - you could probably also add them to the individual elements - thinking about it, that is how I should have done it, but I’m too lazy to go back and redo it! 😃

1. Select loop element
2. Add a PHP Code block from the WP Actions
3. For the function add `$current_year = get_the_date('Y')` and replace Insert before
4. Add a PHP Code 2 block from the WP Actions
5. For the function add `$current_month = get_the_date('F')` and replace Insert before  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/6/6438371e7f8dd692d26309f19d70cc571520fb06.png)

That should do it! If you need additional help reach out.  
Bob

---

_[View the full topic](https://forum.pinegrow.com/t/grouping-posts-by-year-and-month/4718)._
