3

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:

  1. fetch entries in the 'project' section
  2. sort those entries by the 'projectYear' category title (2016->2002)
  3. paginate it (limit 5 for example)
  4. 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.

4

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'
}) %}

<ul>
    {% for category in categories %}
        <li>{{ category.title }}</li>

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

    {% endfor %}
</ul>
  • 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 Mar 17 '16 at 22:24
  • @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 May 2 '16 at 18:04
  • @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 May 7 '16 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.