# Pagination not working with .Reverse

**URL:** <https://discourse.gohugo.io/t/pagination-not-working-with-reverse/35215>\
**Category:** support\
**Tags:** pagination\
**Created:** [October 20, 2021, 3:47am UTC](https://discourse.gohugo.io/t/pagination-not-working-with-reverse/35215 "2021-10-20T03:47:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsphpndr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jsphpndr/32/13697_2.png) [@jsphpndr](https://discourse.gohugo.io/u/jsphpndr)\
**Post date:** [October 20, 2021, 3:47am UTC](https://discourse.gohugo.io/t/pagination-not-working-with-reverse/35215/1 "2021-10-20T03:47:45Z")

</div>

@jmooring, [Pagination Reverse Date Order - Massively Theme](https://discourse.gohugo.io/t/pagination-reverse-date-order-massively-theme/26805)

```auto
{{ $dir := "reports"}}
    
    <div class="[x--wide]">
      {{ $paginator := .Paginate (where .Data.Pages "Section" "reports") }}
      <ul class="[loop] [mb--2]">
        {{ range $paginator.Pages.ByTitle.Reverse }}
        <li class="[loop__item] [flex--between flex--wrap p--1 mb--050]">
          <div>
            <h3 class="[h5 mb--025]"><a href="{{ .Permalink }}">COVID-19 Case Report {{ .Title }}</a></h3>
            <p>{{ .Date | dateFormat "Monday, Jan 2, 2006" }}</p>
          </div>
          <div>
            <a href="{{ .Permalink }}index.json">View JSON</a>
          </div>
        </li>
        {{ end }}
      </ul>
  

```

Posts are titled 515-560. Default pagination works fine and follows the numbering sequence, but `.Reverse` changes the order, but it doesn’t start the listing on the first page from 560, just changes the order of the posts on the page — ie. 515-545 becomes 545-515, 548-557 becomes 557-548.

What I’m looking for is a descending pattern as you progress throughout the paginated pages.

---

<div class="post-metadata">

**Author:** ![jsphpndr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jsphpndr/32/13697_2.png) [@jsphpndr](https://discourse.gohugo.io/u/jsphpndr)\
**Post date:** [October 20, 2021, 3:54am UTC](https://discourse.gohugo.io/t/pagination-not-working-with-reverse/35215/2 "2021-10-20T03:54:49Z")

</div>

@jmooring, here’s the link to the repository: [GitHub - jsphpndr/covid-api-bahamas: This repository was created for COVID-19 data in The Bahamas.](https://github.com/jsphpndr/covid-api-bahamas)

---

<div class="post-metadata">

**Author:** ![jsphpndr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jsphpndr/32/13697_2.png) [@jsphpndr](https://discourse.gohugo.io/u/jsphpndr)\
**Post date:** [October 20, 2021, 3:59am UTC](https://discourse.gohugo.io/t/pagination-not-working-with-reverse/35215/3 "2021-10-20T03:59:35Z")

</div>

@jmooring, you can see how it looks on the frontend here: [COVID-19 Case Reports | COVID-19 API | Bahamas](https://api.covidbahamas.app/reports/)

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [October 20, 2021, 4:31am UTC](https://discourse.gohugo.io/t/pagination-not-working-with-reverse/35215/4 "2021-10-20T04:31:54Z")

</div>

You are invoking pagination three times on the same page:

- [https://github.com/jsphpndr/covid-api-bahamas/blob/main/layouts/reports/list.html#L7](https://github.com/jsphpndr/covid-api-bahamas/blob/main/layouts/reports/list.html#L7)
- [https://github.com/jsphpndr/covid-api-bahamas/blob/main/layouts/reports/list.html#L51](https://github.com/jsphpndr/covid-api-bahamas/blob/main/layouts/reports/list.html#L51)
- [https://github.com/jsphpndr/covid-api-bahamas/blob/main/layouts/reports/list.html#L69](https://github.com/jsphpndr/covid-api-bahamas/blob/main/layouts/reports/list.html#L69)

Don’t do that. See:  
[https://gohugo.io/templates/pagination/#list-paginator-pages](https://gohugo.io/templates/pagination/#list-paginator-pages)

> If you call `.Paginator` or `.Paginate` multiple times on the same page, you should ensure all the calls are identical. Once _either_ `.Paginator` or `.Paginate` is called while generating a page, its result is cached, and any subsequent similar call will reuse the cached result. This means that any such calls which do not match the first one will not behave as written.

After you clean that up, make sure you set the sort order _before_ you paginate.

---

<div class="post-metadata">

**Author:** ![jsphpndr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jsphpndr/32/13697_2.png) [@jsphpndr](https://discourse.gohugo.io/u/jsphpndr)\
**Post date:** [October 20, 2021, 10:33am UTC](https://discourse.gohugo.io/t/pagination-not-working-with-reverse/35215/5 "2021-10-20T10:33:38Z")

</div>

@jmooring, was using this tutorial: [How to build custom Hugo pagination - Glenn McComb: front end developer and designer](https://glennmccomb.com/articles/how-to-build-custom-hugo-pagination/)

I will work to reduce the calls and get back to you.

---

<div class="post-metadata">

**Author:** ![jsphpndr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jsphpndr/32/13697_2.png) [@jsphpndr](https://discourse.gohugo.io/u/jsphpndr)\
**Post date:** [October 20, 2021, 10:44am UTC](https://discourse.gohugo.io/t/pagination-not-working-with-reverse/35215/6 "2021-10-20T10:44:19Z")

</div>

@jmooring, you are a gem.

For anyone else who needs the answer, it’s as follows:

```auto
  {{ $paged := (where .Data.Pages.ByTitle.Reverse "Section" "reports") }}
  {{ $paginator := .Paginate $paged }}

```

Thanks a million, Joe.

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [October 22, 2021, 10:44am UTC](https://discourse.gohugo.io/t/pagination-not-working-with-reverse/35215/7 "2021-10-22T10:44:25Z")

</div>

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