Skip Navigation

[Resolved] How to group posts by month

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to group posts by month

Solution:
If you want to group posts by month using the custom date field, you will have to use the custom shortcode.

You can find the proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/group-results-by-year-and-month-using-date-field/

Relevant Documentation:
=> https://toolset.com/2013/10/how-to-group-views-results-by-year-and-month/

This support ticket is created 2 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 3 voices.

Last updated by Martin 2 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#2363845

Hi,

I have a CPT for events with a dateline field. In a Loop I want to show all upcoming events.

For design purposes I want to „cluster“ all events from month to month. For this I need to determine in the loop of a view that the current event in the loop belongs to the some / a new month like the event before (events are sorted by the date of the event…).

Is there a was to achieve this? Like having a variable to store info in and compare it? Or a function to do so?

Any help to this will help a lot!

Thanks in advance, Martin

#2364185

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Martin

There are a couple of ways you might do this, depending on whether you are more comfortable with PHP or CSS.

The CSS solution would be to add a dynamic class to the container div with the value of the month from the current post (use a shortcode to add the class name).

Then add CSS rules for the even months to give them one colour background, and rules for the odd months to give them a different colour background. Then you'll have a zebra effect. (You'll want to remove margins between the containers to have solid blocks of background colour for each month.)

The PHP alternative would be to declare a custom shortcode that you add to the loop output section. That shortcode would declare a static variable used to store the value of the month of the current loop iteration. A static variable retains its value each time it is called, so when the shortcode runs again (a new loop iteration) it will have the value from the previous iteration, so you can do a simple check to see if the value has changed (from one month to another month), and if it has, return something like an hr tag to provide a visual break between months.

#2364231

Hi Nigel,

Thanks for your fast response! And great to learn about these two options. In my case so like to use the pho option.

Is there an example available hot to set the brindle and how to check for it?

If so, so will be able to adapt it and assign it to my case…

Thanks, Martin

#2364295

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

If you want to group posts by month using the custom date field, here is the reference ticket where I've mentioned the solution:
=> https://toolset.com/forums/topic/group-results-by-year-and-month-using-date-field/

Here is the original post about how you can group posts by year and month but its using standard date field.
=> https://toolset.com/2013/10/how-to-group-views-results-by-year-and-month/

Please let me know if you require further assistance.

#2364337

Dear Minesh, thanks! Great that you jumped in and directed me to this already existing solution. I tried the suggestion and it works perfectly!