How to get a range of all posts in section for a specific year/month

Something like this (which doesnt work or otherwise I wouldnt be here):

{{ $v1 := where .Site.Pages "Section" "photos" }}
{{ $v2 := where (.Site.Pages.Date.Format "2006") $year }}
{{ range $v1 intersect $v2 }}

thanks!

If you’re sharing your entire code block, $year is an undeclared variable.

Also instead of using intersect you could nest your page collections like so:

{{ $pages := where .Site.Pages "Section" "photos" }}
{{ $pages = where ($pages.Date.Format "2006") $year }}
{{ range $pages }}

Ah no, I have $year declared elsewhere.

Your suggestion doesn’t solve my problem btw (same issue, “at <$pages.Date.Format>: can’t evaluate field Date in type page.Pages”), but thanks!

@roytang
Right. I see…

What you are asking is slightly complicated but certainly doable and I would go about it ike so:

{{ $pageGroup := where .Site.RegularPages "Section" "photos" }}
{{ range $pageGroup }}
{{ range .Pages }}
{{ if strings.Contains .Date $year }}
...
{{ end }}
{{ end }}

Basically we create a page group variable to hold the section’s collection.
Then we use the range function twice.
First time to go through the group and the second time to render the actual pages.
Then -and this is where the magic happens- we will use the undocumented strings.Contains function to check whether the year we have stored in the $year variable is present within the date string of the pages.

That’s about it.

Unless of course the people I tag below have a simpler way to do this.

cc: @regis @bep

See https://gohugo.io/templates/lists/#by-date-1