<p>You were close!  In Craft 3, <code>postDate</code> is a full PHP <a href="https://secure.php.net/manual/en/class.datetime.php" rel="nofollow noreferrer">DateTime object</a>, so it'd need to be something like:</p>

<pre><code>{% set allEntriesByYear = allEntries|group('postDate|date("Y")') %}
</code></pre>

CC BY-SA 4.0
2018-06-15T23:41:40.863Z
2018-06-15T23:41:40.863Z
Brad Bell
<p>I am attempting to replicate the example from <a href="https://docs.craftcms.com/v3/templating/filters.html#group" rel="nofollow noreferrer">https://docs.craftcms.com/v3/templating/filters.html#group</a></p>

<p>However, I am unable to access the <code>{{ year }}</code> in the first for loop inside the h2 element. It just shows up as an empty string. Anyone have an idea whats going on?</p>

<p>Here is my code:</p>

<pre><code>{% set allEntries = craft.entries.section('weeklyDigests').all() %}
{% set allEntriesByYear = allEntries|group('postDate.year') %}

{% for year, entriesInYear in allEntriesByYear %}
   &lt;h2&gt;{{ year }}&lt;/h2&gt;

   &lt;ul&gt;

   {% for entry in entriesInYear %}
    &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
   {% endfor %}

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

<templating><craft3><filter>
Craft 3 Docs Group Filter Not Working
# Craft 3 Docs Group Filter Not Working

(Asked by: username_0 on 2018-06-12)

I am attempting to replicate the example from https://docs.craftcms.com/v3/templating/filters.html#group
However, I am unable to access the {{ year }} in the first for loop inside the h2 element. It just shows up as an empty string. Anyone have an idea whats going on?
Here is my code:
{% set allEntries = craft.entries.section('weeklyDigests').all() %}
{% set allEntriesByYear = allEntries|group('postDate.year') %}

{% for year, entriesInYear in allEntriesByYear %}
   {{ year }}

   

   {% for entry in entriesInYear %}
    {{ entry.title }}
   {% endfor %}

   
{% endfor %}

**Tags:** templating, craft3, filter


---

## 1 answers

### Answer by username_1 on 2018-06-15. Score: 1 | Accepted answer

You were close!  In Craft 3, postDate is a full PHP DateTime object, so it'd need to be something like:
{% set allEntriesByYear = allEntries|group('postDate|date("Y")') %}


---

