Where .Date.Year not filtering. Trying to simplify some code

Once again running into an issue that may be from my rather poor reading ability
I’m trying to get a selection of pages based on a year input. “2021” for example so that I can paginate it and loop through in batches of X. However I am getting stuck on the date. Neither option below seems to work despite my understanding that the outer where should allow me to filter based on the context of the pages of the inner where.

{{ range where (where .Site.Pages "Section" "posts") .Date.Year "2021" }}
{{range where (where .Site.Pages "Section" "posts") (.Date.Format "2006") "ge" "2021" }}

If I remove the outer where statement and range through it the context .Date.Format comes out right and I can slice by it and then range over the slice but it seems like a workaround rather than the proper way. Plus I have yet to test pagination for X pages using the slice.

{{ $archYear := .Date.Format “2006” }}
{{ $posts := slice}}
{{ $annualPosts := where .Site.Pages “Section” “posts” }}
{{range $annualPosts }}
{{if eq (.Date.Format “2006”) $archYear}}
{{ $posts = $posts | append . }}
{{end}}
{{end}}

I appreciate any help and any explanation or pointing in the right direction.

.Date.Year returns an integer. You are comparing it to a string.

Yes, I had tried casting it to int and time.Format as well. which I should have mentioned. but still nothing which made me think I missed something trivial as the inner where has a len of 49. It only returns when i use "ne" which returns a len of 49 whilst "ge" and "le" return 0.

At the end of the day The longer block is working, I just was wondering if I had some misunderstanding of how nested where commands worked.

Works for me…

{{ range where (where .Site.RegularPages "Section" "posts") "Date.Year" 2021 }}

Thanks @jmooring I was missing quotes around “Date.Year” :man_facepalming:

Some days man…some days.

1 Like

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