Skip Navigation

[Resolved] Group results by year and month using date field

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

Problem:
Group results by year and month using date field

Solution:
You should use the same shortcode shared with the article here and add it to your current theme's functions.php file:

You should call the shortcode within your view's loop:

<wpv-loop>
          [heading condition="year" value="[types field='stud-registration-date' style='text' format='Y'][/types]"]
            <h4>[types field='stud-registration-date' style='text' format='Y'][/types]</h4>
        [/heading]
  
        [heading condition="month" value="[types field='stud-registration-date' style='text' format='F'][/types]"]
            <h5>[types field='stud-registration-date' style='text' format='F'][/types]</h5>
        [/heading]
  
          <li>    <h4>[wpv-post-title]</h4> </li>
     
        </wpv-loop>

you can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/group-results-by-year-and-month-using-date-field/#post-1074869

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

This support ticket is created 6 years, 5 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 2 voices.

Last updated by dmfzw 6 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#1074268

Tell us what you are trying to do?

I'm trying to create a list of posts ordered by year and month as outlined in this post: https://toolset.com/2013/10/how-to-group-views-results-by-year-and-month/ - however, I would like the posts ordered by date-field and not post-date.

How would I alter the above instructions to group views by month and year from the date-field?

Is there any documentation that you are following?

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

Is there a similar example that we can see?

What is the link to your site?

Site is under development

Regards,
Del Morgan

#1074869

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - you should use the same shortcode without any change and add it to your current theme's functions.php file:

add_shortcode('heading', 'my_heading');
 
function my_heading($atts, $content = '') {
  static $year = null;
  static $month = null;
  $condition = $atts['condition'];
  $value = $atts['value'];
  switch ($condition) {
    case 'year':
    case 'month':
      if ($$condition != $value) {
        $$condition = $value;
        return $content;
      }
      break;
  }
  return '';
}

And here is how you should call the shortcode within your view's loop:

<wpv-loop>
          [heading condition="year" value="[types field='stud-registration-date' style='text' format='Y'][/types]"]
			<h4>[types field='stud-registration-date' style='text' format='Y'][/types]</h4>
		[/heading]
 
		[heading condition="month" value="[types field='stud-registration-date' style='text' format='F'][/types]"]
			<h5>[types field='stud-registration-date' style='text' format='F'][/types]</h5>
		[/heading]
 
          <li>	<h4>[wpv-post-title]</h4> </li>
	
		</wpv-loop>

Where:
- The only change is we are passing the custom date field instead of post date field.
- Please change the field name stud-registration-date with your original field name.

#1074989

Thanks Minesh, that worked.

I modified it to display posts by "day" as well, though this produces the date number without the month before each date number.

This is what happens:

==
2018
AUGUST

6

sample text
sample text

7

sample text

8
sample text
sample text
==

However, I want the month to repeat with each day, eg:

==

AUGUST 2018

August 6

sample text
sample text

August 7

sample text

August 8
sample text
sample text

==

Assuming this is possible, how would I achieve this, so that the month displays with each new date grouping?

Thanks

#1075002

Minesh
Supporter

Languages: English (English )

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

Well - I used following code in view's loop - no change in code you added to functions.php file:

<wpv-loop>
          [heading condition="year" value="[types field='stud-registration-date' style='text' format='F'][/types] [types field='stud-registration-date' style='text' format='Y'][/types]"]
			<h4>[types field='stud-registration-date' style='text' format='F'][/types] [types field='stud-registration-date' style='text' format='Y'][/types]</h4>
		[/heading]
 
		[heading condition="month" value="[types field='stud-registration-date' style='text' format='F'][/types] [types field='stud-registration-date' style='text' format='j'][/types]"]
			<h5>[types field='stud-registration-date' style='text' format='F'][/types] [types field='stud-registration-date' style='text' format='j'][/types]</h5>
		[/heading]
 
          <li>	<h4>[wpv-post-title]</h4> </li>
	
		</wpv-loop>

Where:
- Please change the field name stud-registration-date with your original field name.

I see its working. Could you please confirm 🙂

#1075114

Perfect, thanks Minesh.