I’ve tried several different constructs as shown below, but I can’t get this statement to work. No results are shown. If I change “lt” to “!=” it works, but gives the wrong results. What am I doing wrong?
Front Matter
+++
date = 2020-06-01T08:00:00+08:00
event_date = 2020-06-11T08:00:00+08:00
title = "My Event"
+++
.Params.event_date is set in archtypes: {{(.Date.AddDate 0 0 10).Format "2006-01-02"}}.
{{$pages := where .Pages .Params.event_date "lt" now}}
{{$pages := where .Pages (time .Params.event_date) "lt" now}}
{{$pages := where .Pages (time .Params.event_date) "lt" (now.Format "2006-01-02T15:04:05-0700")}}
{{$pages := where .Pages (time .Params.event_date) "lt" (now.Format "2006-01-02")}}
{{ range .Pages.ByParam "event_date" }}
{{ if .Params.event_date.Before now }}
<h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
<p>{{ .Summary }}</p>
{{ end }}
{{ end }}
You don’t need to cast event_date to time. Hugo sees an unquoted TOML timestamp as type time.Time. If you were to use JSON, YAML, or a quoted value in TOML, you would need to cast to time before doing the comparison.
EDIT: I don’t think there’s any way of doing this with a where clause without converting event_date and now to integers.
I’ve previously got this to work with an if statement, but was hoping to understand why this doesn’t work with a where clause as in my OP. I’ll look into converting the dates to integers and see if that works.