<p>For this kind of thing, I don't see much advantage of using a category relationship for the years (unless each category actually has further meta data or content held against it)  - I'd probably just use a normal number field and get the editors to type in the year (or select from a pre-made dropdown if the range is reasonable)... But that doesn't help you much for your current situation.</p>
<p>How about using the Preparse plugin to populate a hidden field with the year from the chosen category on save? Then you can use a regular <code>orderBy()</code> parameter, which in turn will work with Craft's native pagination.</p>
<p>Alternatively you could use the <code>group</code> filter and then manually build some pagination functionality using segment logic, routes, and <code>offset</code> (or perhaps <code>slice</code>)... but that sounds like a really complex pain... I wouldn't even attempt that to be honest.</p>

CC BY-SA 4.0
2021-04-20T12:56:29.103Z
You know I think you're right that I should just convert these categories to a select field. I've imported them using FeedMe from an XML feed, so it shouldn't be too hard to pull in the values. Thanks.
Mike Mella
CC BY-SA 4.0
2021-04-18T20:12:23.120Z
2021-04-18T20:12:23.120Z
James Smith
<p>You could sort using the <code>multisort</code> filter (<a href="https://craftcms.com/docs/3.x/dev/filters.html#multisort" rel="nofollow noreferrer">docs</a>) then paginate using <a href="https://www.yiiframework.com/doc/api/2.0/yii-data-arraydataprovider" rel="nofollow noreferrer">Yii's ArrayDataProvider</a> - <a href="https://craftcms.stackexchange.com/questions/33882/paginate-entries-grouped-by-day/33885">see this post for reference</a>.</p>
<pre class="lang-twig prettyprint-override"><code>{# Fetch your entries and order by theYear descending #}
{% set allEntriesByYear = craft.entries()
    .section('library')
    .orderBy('postDate DESC')
    .theYear(':notempty:')
    .with(['theYear'])
    .all() | multisort(e =&gt; e.theYear[0], direction=SORT_DESC) %}

{# Create your custom pagination #}
{% set dataProvider = create({
    'class': 'yii\\data\\ArrayDataProvider',
    'allModels': allEntriesByYear,
    'pagination': {'pageSize': 4}}) %} {# Change this to the number you need per page #}

{% set results = dataProvider.getModels() %}

{% set currentPage = dataProvider.getPagination().getPage() + 1 %}
{% set totalPages = dataProvider.getPagination().getPageCount() %}

{% if currentPage &gt; 1 %}
    {% set prevPage = currentPage - 1 %}
{% endif %}
{% if currentPage &lt; totalPages %}
    {% set nextPage = currentPage + 1 %}
{% endif %}

{% if results | length %}
    {% for entry in results %}
        {{ entry.title }} - {{ entry.theYear[0] }}&lt;br&gt;
    {% endfor %}
{% endif %}
&lt;hr&gt;
&lt;div&gt;
    {% if prevPage is defined %}
        &lt;a href=&quot;{{ url(craft.app.request.absoluteUrl, {'q': craft.app.request.get('q'), 'page': prevPage}) }}&quot;&gt;&lt; Previous page&lt;/a&gt;
    {% endif %}

    {% if nextPage is defined %}
        &lt;a href=&quot;{{ url(craft.app.request.absoluteUrl, {'q': craft.app.request.get('q'), 'page': nextPage}) }}&quot;&gt; - Next page &gt;&lt;/a&gt;
    {% endif %}
&lt;/div&gt;
</code></pre>

CC BY-SA 4.0
2021-04-20T12:57:21.340Z
Getting into Yii stuff is a little more than I wanted to do but this is a great suggestion.
Mike Mella
CC BY-SA 4.0
2021-04-20T13:32:41.230Z
Ah, fair enough :)
Oli
CC BY-SA 4.0
2021-04-20T15:48:45.970Z
This is really neat, being able to invoke the pagination functionality on an arbitrary array without a huge mass of complicated offsets/slicing. Will probably need this one day.
James Smith
CC BY-SA 4.0
2021-04-19T11:09:44.513Z
2021-04-19T11:09:44.513Z
Oli
<p>I have a section called <code>Library</code>, and a category group called <code>libraryYears</code>. The categories in that group are just years (<code>2021</code>, <code>2020</code>, <code>2019</code>...)</p>
<p>Each entry in the Library is related to a year in the <code>libraryYears</code> via a related Entries field called <code>theYear</code>.</p>
<p>I want to list all Library entries in order of the related year, so 2021 entries first, followed by 2020 entries, and so on. I don't need year headings, I just need them to be in order of year. But I also want to paginate them so there are only 10 per page.</p>
<p>I tried toying around with the <code>group</code> filter, but can't get it going with pagination.</p>

<categories><pagination><group>
Order entries by related "Year" category and paginate?
# Order entries by related "Year" category and paginate?

(Asked by: username_0 on 2021-04-16)

I have a section called `Library`, and a category group called `libraryYears`. The categories in that group are just years (`2021`, `2020`, `2019`...)

Each entry in the Library is related to a year in the `libraryYears` via a related Entries field called `theYear`.

I want to list all Library entries in order of the related year, so 2021 entries first, followed by 2020 entries, and so on. I don't need year headings, I just need them to be in order of year. But I also want to paginate them so there are only 10 per page.

I tried toying around with the `group` filter, but can't get it going with pagination.

**Tags:** categories, pagination, group

---

## 2 answers

### Answer by username_1 on 2021-04-18. Score: 0 | Accepted answer

For this kind of thing, I don't see much advantage of using a category relationship for the years (unless each category actually has further meta data or content held against it) - I'd probably just use a normal number field and get the editors to type in the year (or select from a pre-made dropdown if the range is reasonable)... But that doesn't help you much for your current situation.

How about using the Preparse plugin to populate a hidden field with the year from the chosen category on save? Then you can use a regular `orderBy()` parameter, which in turn will work with Craft's native pagination.

Alternatively you could use the `group` filter and then manually build some pagination functionality using segment logic, routes, and `offset` (or perhaps `slice`)... but that sounds like a really complex pain... I wouldn't even attempt that to be honest.

> **Comments:**
> **username_0**: You know I think you're right that I should just convert these categories to a select field. I've imported them using FeedMe from an XML feed, so it shouldn't be too hard to pull in the values. Thanks.  

---

### Answer by username_2 on 2021-04-19. Score: 2

You could sort using the multisort filter (docs) then paginate using Yii's ArrayDataProvider - see this post for reference.
{# Fetch your entries and order by theYear descending #}
{% set allEntriesByYear = craft.entries()
    .section('library')
    .orderBy('postDate DESC')
    .theYear(':notempty:')
    .with(['theYear'])
    .all() | multisort(e => e.theYear[0], direction=SORT_DESC) %}

{# Create your custom pagination #}
{% set dataProvider = create({
    'class': 'yii\\data\\ArrayDataProvider',
    'allModels': allEntriesByYear,
    'pagination': {'pageSize': 4}}) %} {# Change this to the number you need per page #}

{% set results = dataProvider.getModels() %}

{% set currentPage = dataProvider.getPagination().getPage() + 1 %}
{% set totalPages = dataProvider.getPagination().getPageCount() %}

{% if currentPage > 1 %}
    {% set prevPage = currentPage - 1 %}
{% endif %}
{% if currentPage < totalPages %}
    {% set nextPage = currentPage + 1 %}
{% endif %}

{% if results | length %}
    {% for entry in results %}
        {{ entry.title }} - {{ entry.theYear[0] }}
    {% endfor %}
{% endif %}


    {% if prevPage is defined %}
        < Previous page
    {% endif %}

    {% if nextPage is defined %}
         - Next page >
    {% endif %}

> **Comments:**
> **username_0**: Getting into Yii stuff is a little more than I wanted to do but this is a great suggestion.  
> **username_2**: Ah, fair enough :)  
> **username_1**: This is really neat, being able to invoke the pagination functionality on an arbitrary array without a huge mass of complicated offsets/slicing. Will probably need this one day.  

---

