Sorting categories by the post date of the entries related to them

I need to sort my categories based on the date of entries returned within the category. I am trying to sort sermons from a church by the series they were contained within. I have the sermons grouped by their series and outputting correctly on that front but they display in order based on the category creation date.

I need the sermon series to show in chronological descending order.

So we have Sermon Series A, Sermon Series B, Sermon Series C... within each one of those categories we have individual sermons related to the category. For example:

• Sermon Series B

• Sermon 1 (1/10/2013)
• Sermon 2 (1/17/2013)
• Sermon Series C

• Sermon 1 (9/10/2014)
• Sermon 2 (9/17/2014)
• Sermon Series A

• Sermon 1 (5/10/2014)
• Sermon 2 (5/17/2014)

Currently my output looks very similar to the above example but what I need is for it to show like this:

• Sermon Series C
• Sermon 1 (9/10/2014)
• Sermon 2 (9/17/2014)
• Sermon Series A
• Sermon 1 (5/10/2014)
• Sermon 2 (5/17/2014)
• Sermon Series B
• Sermon 1 (1/10/2013)
• Sermon 2 (1/17/2013)

Since the entries contained within the categories are in that order by post date.

Here is my current Twig code:

{% set entries = craft.entries.section('sermon').limit(null) %}

{% for category in craft.categories.group('sermonSeries').relatedTo(entries).order('dateCreated desc') %}

{% for entry in entries.relatedTo(category) %}


But as I stated above this doesn't output my content with the desired effect.

Alan,

Without having tested this, you might get the desired result by using the group filter.

So, get all you entries ordered by dateCreated, and then group them by category:

{% set sermons = craft.entries.section('sermon').order('dateCreated desc').limit(null) %}
{% for category, catSermons in sermons | group('categoryFieldHandle.first().title') %}
<li>{{ category }}
{% for sermon in catSermons %}
<li>{{ sermon.title }}</li>
{% endfor %}
</li>
{% endfor %}


Let me know if that works!

• That's a great idea! I tried it out and is giving me the following error: Object of class Craft\ElementCriteriaModel could not be converted to string I was able to return results by using group('categoryFieldHandle.first().title') then I could just get the data related to that category by making another database call. I do wonder if there is a way I can return the whole CategoryModel with this group parameter. Anyone have any ideas? – Alan Miller Sep 19 '14 at 20:12
• You can get the CategoryModel from any one of the sermons, you could set it from the first sermon for each category like: {% if categoryModel is not defined or categoryModel.title is not category %} {% set category = sermon.categoryFieldHandle %} {% endif %} or something like that. No extra query required ... – Paul Sep 19 '14 at 20:18

In this case, I'd probably use the group filter as described in other answers.

However, if you had sermons in multiple categories, only grouping them under their first category might not work.

In that case, you could query the categories first, and use my SuperSort plugin to sort them based on a value computed by Twig, such as the number of related entries. (SuperSort allows you to render each object in an array using a Twig object template and sort the array based on the rendered value.)