<p>In your code, within the <code>{% for eventDate, entries in eventEntries | group("dayStart|date('F Y')") %}</code> loop, in the sub-loop, you are looping over <code>eventEntries.relatedTo(category).find()</code> rather than restricting the entries in that loop to the ones you have already grouped to the date, i.e. in the <code>entries</code> 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.</p>

<p>This should work:</p>

<pre><code>{% 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 %}
</code></pre>

CC BY-SA 3.0
2017-02-16T13:11:34.507Z
I'm getting an 'Array to string conversion' error when I try this, when I remove '.relatedTo(category).find()' it works but without being filtered by category. What's another way to add a 'relatedTo' parameter to {% for entry in entries %} without errors?
T.Newton
CC BY-SA 3.0
2017-02-16T13:15:26.730Z
What's the name of your categories field in the entry?
Steve Rowling
CC BY-SA 3.0
2017-02-16T13:20:32.947Z
Try the updated answer. I've added a conditional to only loop through entries which have the current category (from the outer loop) in their categoriesField (put the field handle of your category field here).
Steve Rowling
CC BY-SA 3.0
2017-02-16T13:33:45.337Z
My category's field name is 'category'. I tried your updated answer and it worked! Thanks so much, you wonderful human being!
T.Newton
CC BY-SA 3.0
2017-02-16T11:39:48.320Z
2017-02-16T13:18:21.757Z
2017-02-16T13:18:21.757Z
Steve Rowling
<p>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:</p>

<ul>
<li>Category 1

<ul>
<li>February 2017

<ul>
<li>Event 1</li>
</ul></li>
<li>March 2017

<ul>
<li>Event 4 (in all categories)</li>
</ul></li>
</ul></li>
<li>Category 2

<ul>
<li>February 2017

<ul>
<li>Event 2</li>
<li>Event 5</li>
</ul></li>
<li>March 2017

<ul>
<li>Event 4 (in all categories)</li>
</ul></li>
</ul></li>
<li>Category 3

<ul>
<li>March 2017

<ul>
<li>Event 4 (in all categories)</li>
</ul></li>
</ul></li>
</ul>

<p>Here is what I'm currently using:</p>

<pre><code>{% 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 %}
</code></pre>

<p>Instead the output I'm getting is this:</p>

<ul>
<li>Category 1

<ul>
<li>February 2017

<ul>
<li>Event 1</li>
<li>Event 4 (in all categories)</li>
</ul></li>
<li>March 2017

<ul>
<li>Event 1</li>
<li>Event 4 (in all categories)</li>
</ul></li>
</ul></li>
<li>Category 2

<ul>
<li>February 2017

<ul>
<li>Event 2</li>
<li>Event 5</li>
<li>Event 4 (in all categories)</li>
</ul></li>
<li>March 2017

<ul>
<li>Event 2</li>
<li>Event 5</li>
<li>Event 4 (in all categories)</li>
</ul></li>
</ul></li>
<li>Category 3

<ul>
<li>February 2017

<ul>
<li>Event 4 (in all categories)</li>
</ul></li>
<li>March 2017

<ul>
<li>Event 4 (in all categories)</li>
</ul></li>
</ul></li>
</ul>

<p>Is there a way to stop the duplicates?</p>

<p>Thanks in advance.</p>

<templating><categories><datetime>
Group entries by category than by month based on Date/Time field
# Group entries by category than by month based on Date/Time field

(Asked by: username_0 on 2017-02-16)

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 %}
    {{ category.title }}
        {% for eventDate, entries in eventEntries | group("dayStart|date('F Y')") %}
            {{ eventDate }}
            
                {% for entry in eventEntries.relatedTo(category).find() %}
                    {{ entry.title }}
                {% endfor %}
            
        {% 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.

**Tags:** templating, categories, datetime

---

## 1 answers

### Answer by username_1 on 2017-02-16. Score: 2 | Accepted answer

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 %}
    {{ category.title }}
        {% for eventDate, entries in eventEntries | group("dayStart|date('F Y')") %}
            {{ eventDate }}
            
                {% for entry in entries if (category.id in entry.categoriesField.ids()) %}
                    {{ entry.title }}
                {% endfor %}
            
        {% endfor %}
{% endfor %}

> **Comments:**
> **username_0**: I'm getting an 'Array to string conversion' error when I try this, when I remove '.relatedTo(category).find()' it works but without being filtered by category. What's another way to add a 'relatedTo' parameter to {% for entry in entries %} without errors?  
> **username_1**: What's the name of your categories field in the entry?  
> **username_1**: Try the updated answer. I've added a conditional to only loop through entries which have the current category (from the outer loop) in their categoriesField (put the field handle of your category field here).  
> **username_0**: My category's field name is 'category'. I tried your updated answer and it worked! Thanks so much, you wonderful human being!  

---

