Sorting/ordering months in a calendar loop

What I am trying to do:
Showing posts, segmented by year and month in relation to when they are posted.

I am very close to that goal. I just need to sort the months in an descending order, so that the newest posts are effectively at the top. Since, you know, time is linear and such.

This is where I am at:

But, I just cannot figure out how to sort the months. I assume this is because it is an attribute loop (or loop without a type).

Here is a basic version of my current code:

<Loop type=calendar_year from=2021 to=current sort_field="year" sort_type="number" sort_order="desc">
  <Date all_locale=da_DK />
  <h1><Field year /></h1>
  <Loop field=month> // <--- this is where I need some sorting to happen
    <Field name />
    <Loop type="post" publish_month="{Field month}" publish_year="{Field year}" orderby=date order=desc>
      <Field title />
    </Loop>
  </Loop>
</Loop>

The above code produces the following output, where a demo post is shown:

What is the smartest way to do this?

Cheers

:joy:

Does using sort_order="desc" again on the month field loop work? I’m not sure if it could just invert the order of the looped items without specifying a field. I doubt it, but it might be worth a shot.

<Loop field=month sort_order=desc>

If that doesn’t work, you couldn’t do number-based sorting as you did with your outer year loop but maybe date sorting would work, like this:

<Loop field=month sort_field="month" sort_type="date" sort_order="desc">

I haven’t tried this myself but I’d be curious to hear what you find.

Hey Benjamin!

Sadly none of these work. I tried many combinations of it. Since <Field month /> returns a single numerical value between 1 and 12, I also tried sort_type=“number”. But I don’t think any of them ever had any effect.

Hmm, I’m surprised that those attributes don’t work on field loops. I wonder if that’s the case for all field loops or if there’s something unique about calendar loops. Seems like it would warrant some tinkering.

I think you could simply redefine a month loop based on the current year from the outer loop. I can’t imagine calendar loops take very much processing power to generate so I doubt the performance impact would be significant. Either of these should work:

<Loop type=calendar_month year="{Field year}" sort_field="month" sort_type="number" sort_order="desc">
<Loop type=calendar_month year="{Field year}" sort_field="name" sort_type="date" sort_order="desc">

In an ideal world, I imagine it shouldn’t be necessary to use attributes to reorder the items in a calendar loop. I think the main thing that needs to happen is that there needs to be an attribute available on all the different calendar loops that allow them to be generated in reverse (descending order). I’ve created a feature request for that.

1 Like

This worked! Thanks!

For anyone who stumbles upon this:
I ended up with this, which loops out the years and then the months in descending order:

<Loop type=calendar_year from=2021 to=current sort_field="year" sort_type="number" sort_order="desc">
  <Loop type=calendar_month year="{Field year}" sort_field="month" sort_type="number" sort_order="desc">
    <Field name />
    <Loop type="post" publish_month="{Field month}" publish_year="{Field year}" orderby=date order=desc>
      <Field title />
    </Loop>
  </Loop>
</Loop>