Date sorting not working properly?

Hi all,

I’m working on a website where there is a list of news items, and each news item can be associated with a specific person. Each person has their own page, and I want all associated news items to appear on the respective pages.

I’ve got the basic functionality working, as far as displaying the correct news items, but they are not being sorted in the proper chronological order. Hoping someone can spot the issue!

Also, very likely this code is very ugly and I’m going about this all wrong. Extremely open to suggestions in general to make things better overall.

{{ $last_name := .Params.last_name }}

{{ range sort (where $.Site.Pages "Section" "news") ".Params.article_date" "desc" }}

      {{ $link := .Params.article_url }}
      {{ $title := .Params.title }}
      {{ $date := .Params.article_date }}

      {{ range .Params.gallery_artists }}

        {{ if eq .fields.lastName $last_name }}

            <td><a href="{{ $link }}">{{ $title }}</a></td>
            <td>{{ dateFormat "01.02.2006" $date }}</td>

        {{ end }}

      {{ end }}

{{ end }}

The test news items I have are showing up as:

Publication Date
Artforum 06.20.2022
Artland 10.01.2020
Texte zur Kunst 06.01.2022


Thank you!

Hugo supports .Date, .ExpiryDate, .PublishDate, and .Lastmod in a content’s front matter. Read more here

so, I guess this part should be like this, if you replace article_date with date in your front matter

{{ range sort (where site.Pages.ByDate.Reverse "Section" "news")  }}

Oh wow, yup that was it. I changed the “article_date” to “date” and now it sorts perfectly with the following:

{{ range where $.Site.Pages "Section" "news" }}

Thank you, @tut !

Changing all news items from “article_date” to just “date” seemingly introduced another weird unexpected behavior.

My “/content/news” folder:

Output when I try to get all news items:

{{ range where $.Site.Pages "Section" "news" }}

     <li>{{ . }}</li>

{{ end }}

Any thoughts on why it would only return those four?? Before the front matter date name change it returned all of them. I can’t possibly think of why that would happen. The .md files are generated from a CMS using a script, so they are all structured identically.

I changed it back to “article_date” in the front matter, and changed all corresponding code, and it’s loading all news items again. I am so confused.

Share your repository for further assistance