<p>Not exactly what you're looking for, but it gets you closer.  This should display the order you want, but it doesn't have any pagination (and honestly I'm not sure if that's possible using Craft's <code>{% paginate %}</code> tag along with your other requirements.</p>

<p>IMO, you're probably going to be better off doing this in PHP either using a <a href="https://craftcms.com/classreference/services/TemplatesService#hook-detail" rel="nofollow">Template Hook</a> or a <a href="https://craftcms.com/docs/plugins/introduction" rel="nofollow">custom plugin</a>.</p>

<p>Regardless, here's the Twig code:</p>

<pre><code>{% set allProjects = craft.entries({
    section: 'projects',
    order: 'title asc'
}) %}

{% set categories = craft.categories({
    relatedTo: allProjects,
    title: 'not Unknown',
    order: 'title desc'
}) %}

&lt;ul&gt;
    {% for category in categories %}
        &lt;li&gt;{{ category.title }}&lt;/li&gt;

        {% set relatedEntries = craft.entries.relatedTo(category).order('title asc') %}
        &lt;ul&gt;
            {% for entry in relatedEntries %}
                &lt;li&gt;{{ entry.title }}&lt;/li&gt;
            {% endfor %}
        &lt;/ul&gt;

    {% endfor %}
&lt;/ul&gt;
</code></pre>

CC BY-SA 3.0
2016-03-17T22:24:21.957Z
Thanks Brad!
Your solution definitely gets me closer but I'll follow you advice and will develop a template hook or a plugin.
Cheers!
Pierre Stoffe
CC BY-SA 3.0
2016-05-02T18:04:30.110Z
@PierreStoffe, did you end up finding a solution for this? I am trying to do something similar and would love to see your solution, if you found one. Thanks!
Justin K
CC BY-SA 3.0
2016-05-07T07:55:02.107Z
@JustinK Not really, sadly. I ended up using the postDate instead of a custom field so I wouldn't consider this a solution per say.
Pierre Stoffe
CC BY-SA 3.0
2016-03-16T01:22:59.980Z
2016-03-16T01:22:59.980Z
Brad Bell
<p>I'm stuck on an issue where I have to paginate a list of entries that would be sorted by category title.
So basically, I need to:</p>

<ol>
<li>fetch entries in the 'project' section</li>
<li>sort those entries by the 'projectYear' category title (2016->2002)</li>
<li>paginate it (limit 5 for example)</li>
<li>sort entries by title (a->z)</li>
</ol>

<p>The output expected would look like this</p>

<pre><code>Page 1
    2016
        Project A
        Project B
        Project C
    2015
        Project D
        Project E
Page 2
    2015
        Project F
        Project G
    2012
        Project H
    2010
        Project I
        Project J
</code></pre>

<p>At the moment, I have this "working"</p>

<pre><code>{% set categories = craft.categories({
    group: 'projectYear',
    title: 'not Unknown',
    order: 'title desc'
}) %}

{% set allProjects = craft.entries({
    section: 'projects',
    relatedTo: categories,
    order: 'title asc'
}) %}

{% for year, projectsInYear in allProjects | group('projectYear.first().title') %}
    {{ year }}
    {% for project in projectsInYear %}
        {{ project.title }}
    {% endfor %}
{% endfor %}
</code></pre>

<p>By "working" I mean that it outputs all project entries sorted by title &amp; grouped by projectYear... but those projectYear groups are not sorted by title and it also lacks a pagination system.</p>

<p>FYI: If I limit to 5 in craft.entries, it outputs the 5 first entries sorted by title and groups them by year after that. Instead, I want it entries to be first grouped by year and then get the 5 of those first entries.</p>

<p>Could anybody help me figure out how I can get the output I want or lead me in the right direction?</p>

<p>Thanks!</p>

<p>PS: we used postDate originally and it worked great, but we had to move from using postDate so it's not a possibility anymore.</p>

<categories><ordering><group><sort>
Output entries sorted by category title and paginate
# Output entries sorted by category title and paginate

(Asked by: username_0 on 2016-03-15)

I'm stuck on an issue where I have to paginate a list of entries that would be sorted by category title.
So basically, I need to:

fetch entries in the 'project' section
sort those entries by the 'projectYear' category title (2016->2002)
paginate it (limit 5 for example)
sort entries by title (a->z)

The output expected would look like this
Page 1
    2016
        Project A
        Project B
        Project C
    2015
        Project D
        Project E
Page 2
    2015
        Project F
        Project G
    2012
        Project H
    2010
        Project I
        Project J

At the moment, I have this "working"
{% set categories = craft.categories({
    group: 'projectYear',
    title: 'not Unknown',
    order: 'title desc'
}) %}

{% set allProjects = craft.entries({
    section: 'projects',
    relatedTo: categories,
    order: 'title asc'
}) %}

{% for year, projectsInYear in allProjects | group('projectYear.first().title') %}
    {{ year }}
    {% for project in projectsInYear %}
        {{ project.title }}
    {% endfor %}
{% endfor %}

By "working" I mean that it outputs all project entries sorted by title & grouped by projectYear... but those projectYear groups are not sorted by title and it also lacks a pagination system.
FYI: If I limit to 5 in craft.entries, it outputs the 5 first entries sorted by title and groups them by year after that. Instead, I want it entries to be first grouped by year and then get the 5 of those first entries.
Could anybody help me figure out how I can get the output I want or lead me in the right direction?
Thanks!
PS: we used postDate originally and it worked great, but we had to move from using postDate so it's not a possibility anymore.

**Tags:** categories, ordering, group, sort

---

## 1 answers

### Answer by username_1 on 2016-03-16. Score: 4 | Accepted answer

Not exactly what you're looking for, but it gets you closer.  This should display the order you want, but it doesn't have any pagination (and honestly I'm not sure if that's possible using Craft's {% paginate %} tag along with your other requirements.
IMO, you're probably going to be better off doing this in PHP either using a Template Hook or a custom plugin.
Regardless, here's the Twig code:
{% set allProjects = craft.entries({
    section: 'projects',
    order: 'title asc'
}) %}

{% set categories = craft.categories({
    relatedTo: allProjects,
    title: 'not Unknown',
    order: 'title desc'
}) %}


    {% for category in categories %}
        {{ category.title }}

        {% set relatedEntries = craft.entries.relatedTo(category).order('title asc') %}
        
            {% for entry in relatedEntries %}
                {{ entry.title }}
            {% endfor %}
        

    {% endfor %}

> **Comments:**
> **username_0**: Thanks Brad! Your solution definitely gets me closer but I'll follow you advice and will develop a template hook or a plugin. Cheers!  
> **username_2**: @PierreStoffe, did you end up finding a solution for this? I am trying to do something similar and would love to see your solution, if you found one. Thanks!  
> **username_0**: @JustinK Not really, sadly. I ended up using the postDate instead of a custom field so I wouldn't consider this a solution per say.  

---

