Order entries by related "Year" category and paginate? 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.
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 =&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;