Grouping Posts by Year and Month

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

@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

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

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! :smiley:

  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

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

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 :slight_smile:

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.

1 Like

Sehr schön!! Coole Nutzung.

Danke :slight_smile:

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

1 Like

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?

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

1 Like

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