Select Pages using `where` fails but `if` works with date comparison

I have this project structure after running hugo new and adding minimal working files:

project
│   config.yaml    
│
└───layouts
│   │   index.html
│   │
│   └───_default
│       │   baseof.html
│   
│   └───layouts
│       │   head.html
│       │   recent.html
│       │   recent-filter.html
│       │   script-footer.html
│   
└───content
│   └───posts
│       │   └───2022-10-31
│       │       │   index.md
│       │   └───2023-05-28
│       │       │   index.md
│   

md:title=layouts/partials/recent.html

<div>
    {{ range sort (where site.RegularPages "Section" "posts") "Params.start" }}
    {{ $dateTime := .Params.start | time.AsTime }}
    {{ $title := .Params.title }}
    <div>
        <p style="font-size:12px">
          <time datetime="{{ $dateTime }}">{{ $dateTime | time.Format ":date_long" }}</time>
          <t/>
          {{ $title }}
      </p>
      </div>
  {{ end }}
</div>

md:title=layouts/partials/recent-filter.html

<div>
    {{ range where (sort (where site.RegularPages "Section" "posts") "Params.start") "Params.start"  "ge" now }}
    {{ $dateTime := .Params.start | time.AsTime }}
    {{ $title := .Params.title }}
    <div>
        <p style="font-size:12px">
          <time datetime="{{ $dateTime }}">{{ $dateTime | time.Format ":date_long" }}</time>
          <t/>
          {{ $title }}
      </p>
      </div>
  {{ end }}
</div>

This results in the following screenshot:

I expected the where clause to include only posts with start date greater equal to today. My use case is to only include ‘the most recent post’ so after i select only the posts after today i will use first 1 on the sorted list. I have unquoted dates in the front matter of markdown and they are all in same format.

I looked at this related post. Using {{ if (time .Params.start).After now }} ... {{ end }} works but I can’t use first 1 with that and I see break is not implemented. I already received advice to code defensively around pages with the start command so I’m looking into that now if that’s related (ty @jmooring).

Code Repository

If your front matter is still YAML, you are comparing a string to a time.Time value.

See https://gohugo.io/templates/introduction/#example-show-future-events

I updated all file frontmatter to toml and unquoted the datetime eg.

+++
draft = 'false'
title = 'Summer'
start = 2023-05-28T00:00:00+11:00
+++

I updated the range statement in recent-filter.html

{{ range where (sort (where site.RegularPages "Section" "posts") .Params.start) .Params.start ge now }}

Now I get the compile error: <ge>: wrong number of args for ge: want at least 1 got 0

Repo updated

"ge" not ge

Replaced ge with "ge" and compiles but doesn’t filter as expected.

Got it. I was missing more quotes. {{ range where (sort (where site.RegularPages "Section" "posts") ".Params.start") ".Params.start" "ge" now }}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.