# 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:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![HoWin](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@HoWin](https://forum.pinegrow.com/u/HoWin)\
**Post date:** [November 25, 2020, 6:16pm UTC](https://forum.pinegrow.com/t/grouping-posts-by-year-and-month/4718/1 "2020-11-25T18:16:32Z")

</div>

Hi

I would be interested if the following is also possible with Pinegrow WP actions:

I need to group my posts by date so that the end result looks like this: (h1 and h2 are headers)

```
h1: 2020
   h2: Jan
           Post 1
           Post 2
           Post 3
            ....
   h2: Feb
           Post 4
           Post 5
            ....
h1: 2019
   h2: Mar
          Post 6
          Post 7
       ....

```

I know how to solve this problem with standard PHP/SQL Queries, but this doesn’t look nice inside Pinegrow.

I would be very happy if you have a tip how I can solve these queries with Pinegrow functions.

Thanks

Horst

---

<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

---

<div class="post-metadata">

**Author:** ![HoWin](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@HoWin](https://forum.pinegrow.com/u/HoWin)\
**Post date:** [November 27, 2020, 3:28pm UTC](https://forum.pinegrow.com/t/grouping-posts-by-year-and-month/4718/3 "2020-11-27T15:28:42Z")

</div>

Hey Bob

Many many thanks. Your reply helped me a lot. Your solution works fantastic. I also solved it before, but my variant was much more complicated and cumbersome.

And best of all: I’ve learned a lot.

Great help. Thank you very much 🙂

My best regards,  
Horst

Find attached the result of your loop. I group entries by “Year-Month” and display them alternating left and right on a vertical timeline.

 ![Auswahl_002](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/e/e76e8c00e6bff1e5f40e9515fc79db479296fa4a.png)

---

<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 27, 2020, 4:56pm UTC](https://forum.pinegrow.com/t/grouping-posts-by-year-and-month/4718/4 "2020-11-27T16:56:24Z")

</div>

Sehr schön!! Coole Nutzung.

---

<div class="post-metadata">

**Author:** ![HoWin](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@HoWin](https://forum.pinegrow.com/u/HoWin)\
**Post date:** [November 27, 2020, 6:12pm UTC](https://forum.pinegrow.com/t/grouping-posts-by-year-and-month/4718/5 "2020-11-27T18:12:14Z")

</div>

Danke 🙂

Pinegrow ist ein Hit, ich entdecke laufend Neues! einfach genial!

---

<div class="post-metadata">

**Author:** ![schpengle](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/schpengle/32/238_2.png) [@schpengle](https://forum.pinegrow.com/u/schpengle)\
**Post date:** [March 22, 2021, 8:51am UTC](https://forum.pinegrow.com/t/grouping-posts-by-year-and-month/4718/6 "2021-03-22T08:51:17Z")

</div>

> [@RobM](#):
>
> Add a PHP Code 2 block from the WP Actions

Hi @RobM , just looking at this now, and towards the beginning, you have the above listed under point No. 6.

Just wondering, whats is _code 2 block_?

---

<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:** [March 22, 2021, 9:27am UTC](https://forum.pinegrow.com/t/grouping-posts-by-year-and-month/4718/7 "2021-03-22T09:27:18Z")

</div>

Hey @schpengle,  
If you look at the bottom of the picture before it you can see the named block - ‘PHP Code 2’. Basically, in the WordPress Actions panel there are multiple blocks for adding code into the same action. They need to be named differently for editing purposes, so 1, 2, 3, etc…  
Cheers,  
Bob

---

<div class="post-metadata">

**Author:** ![schpengle](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/schpengle/32/238_2.png) [@schpengle](https://forum.pinegrow.com/u/schpengle)\
**Post date:** [March 22, 2021, 9:29am UTC](https://forum.pinegrow.com/t/grouping-posts-by-year-and-month/4718/8 "2021-03-22T09:29:43Z")

</div>

ah Gotchya! thanks, yes, I see it now, makes sense.  
slight… reading inability on my end! doh!
