Group entries by category than by month based on Date/Time field I would like group entries first by what category they are in, than group by month based on a Date/Time field like this for example:


Category 1


February 2017


Event 1

March 2017


Event 4 (in all categories)


Category 2


February 2017


Event 2
Event 5

March 2017


Event 4 (in all categories)


Category 3


March 2017


Event 4 (in all categories)




Here is what I'm currently using:

{% set eventCategories = craft.categories.group('events').find() %}
{% set eventEntries = craft.entries.section('event').order('dayStart asc').limit(null) %}
{% for category in eventCategories %}
    &lt;h1&gt;{{ category.title }}&lt;/h1&gt;
        {% for eventDate, entries in eventEntries | group("dayStart|date('F Y')") %}
            &lt;h2&gt;{{ eventDate }}&lt;/h2&gt;
            &lt;ul&gt;
                {% for entry in eventEntries.relatedTo(category).find() %}
                    &lt;li&gt;{{ entry.title }}&lt;/li&gt;
                {% endfor %}
            &lt;/ul&gt;
        {% endfor %}
{% endfor %}


Instead the output I'm getting is this:


Category 1


February 2017


Event 1
Event 4 (in all categories)

March 2017


Event 1
Event 4 (in all categories)


Category 2


February 2017


Event 2
Event 5
Event 4 (in all categories)

March 2017


Event 2
Event 5
Event 4 (in all categories)


Category 3


February 2017


Event 4 (in all categories)

March 2017


Event 4 (in all categories)




Is there a way to stop the duplicates?

Thanks in advance.
In your code, within the {% for eventDate, entries in eventEntries | group("dayStart|date('F Y')") %} loop, in the sub-loop, you are looping over eventEntries.relatedTo(category).find() rather than restricting the entries in that loop to the ones you have already grouped to the date, i.e. in the entries array. That is why you are getting all the entries related to the category under each date heading, instead of just the ones falling under that date.

This should work:

{% set eventCategories = craft.categories.group('events').find() %}
{% set eventEntries = craft.entries.section('event').order('dayStart asc').limit(null) %}
{% for category in eventCategories %}
    &lt;h1&gt;{{ category.title }}&lt;/h1&gt;
        {% for eventDate, entries in eventEntries | group("dayStart|date('F Y')") %}
            &lt;h2&gt;{{ eventDate }}&lt;/h2&gt;
            &lt;ul&gt;
                {% for entry in entries if (category.id in entry.categoriesField.ids()) %}
                    &lt;li&gt;{{ entry.title }}&lt;/li&gt;
                {% endfor %}
            &lt;/ul&gt;
        {% endfor %}
{% endfor %}