Paginate, Exclude Latest Posts AND Group by Date?

I’m building an archive page, wherein I have three criteria:

  • Paginate the pages
  • Exclude the ten newest posts [as these are already on a separate ‘blog’ page].
  • Group the remaining posts under Date headings.

This works to paginate AND exclude the ten newest posts:

{{ $paginator := .Paginate (where .Data.Pages "Type" "post") }}
 
{{ range after 10 $paginator.Pages }}

//show posts

{{end}}

This works to paginate AND group the posts under Date headings:

{{ $paginator := .Paginate (where .Data.Pages "Type" "post") }}

{{ range $paginator.Pages.GroupByDate "2006 Jan" }}

<h3>{{ .Key }}</h3>

{{ range .Pages }}

//show posts

{{end}}
{{end}}

However, I’m having difficulties achieveing all three at once. The closest I’ve got is this:

{{ $paginator := .Paginate (where .Data.Pages "Type" "post") }}

{{ range $paginator.Pages.GroupByDate "2006 Jan" }}

<h3>{{ .Key }}</h3>

{{ range after 10 .Pages }}

//show posts

{{end}}
{{end}}

Which works, up to a point, but still shows the GroupByDate headings for the ten posts which have been excluded using after. In other words, I still get the date headings with nothing underneath, where those posts would have been, if I’d not filtered them out.

If I try to combine the after and GroupByDate directives in one:

{{ $paginator := .Paginate (where .Data.Pages "Type" "post") }}

{{ range after 10 $paginator.Pages.GroupByDate "2006 Jan" }}

<h3>{{ .Key }}</h3>

{{ range .Pages }}

//show posts

{{end}}
{{end}}

Hugo doesn’t throw any errors, but none of the posts are displayed, at all.

And, finally, just on the off-chance I could pre-filter the newest ten posts at the paginator stage, I tried moving the after directive out to the $paginator call itself. But again, while it didn’t throw up any errors, it resulted in no posts at all being displayed:

{{ $paginator := .Paginate after 10 (where .Data.Pages "Type" "post") }}

{{ range $paginator.Pages.GroupByDate "2006 Jan" }}

<h3>{{ .Key }}</h3>

{{ range .Pages }}

//show posts

{{end}}
{{end}}

I can kludge something together from my “kind of” working version to first check for the existence of a qualifying post before rendering each GroupByDate header. But I was just wondering if there was a more elegant way to do what I want.

Can anyone put me out of my misery?

Can’t test it right now, but does something like this work?

{{ $paginator := .Paginate (after 10 (where .Data.Pages "Type" "post").GroupByDate "2006 Jan") }}
 
{{ range $paginator.Pages }}
//show posts
{{end}}

Not sure the ().GroupByDate syntax is allowed. You may have to break that off into a separate step. The idea is that you want to do all your filtering and sorting before you paginate.

That was the answer.

Like a few of my random permutations, your first version returned no error but no results either. However, keeping the GroupByDate separate worked.

For the record:

{{ $paginator := .Paginate (after 10 (where .Data.Pages "Type" "post")) }}

{{ range $paginator.Pages.GroupByDate "2006 Jan" }}

<h3>{{ .Key }}</h3>

{{ range .Pages }}

//show posts

{{end}}
{{end}}

Thanks for the help with the $paginator syntax. I never seem to have much luck, when I try shoe-horning brackets into my Go template code.

1 Like

The page group support in pagination was a new thing in Hugo 0.15 if my memory serves me correct.