<p>There are lots of different ways to do this. Have you tried anything so far?</p>
<p>You could use the <code>group</code> filter to group all the entries by their <code>postDate</code> year:</p>
<pre class="lang-twig prettyprint-override"><code>{% set entriesGroupedByYear = craft.entries.section('handle').all()|group(entry =&gt; entry.postDate|date(&quot;Y&quot;)) %}

&lt;ol&gt;
    {% for year, entries in entriesGroupedByYear %}
        &lt;li&gt;{{ year }}&lt;/li&gt;
    {% endfor %}
&lt;/ol&gt;

</code></pre>
<p><a href="https://craftcms.com/docs/4.x/dev/filters.html#group" rel="nofollow noreferrer">https://craftcms.com/docs/4.x/dev/filters.html#group</a></p>
<p>This will give you a two-dimensional array where the keys are the years and the values are arrays of entries belonging to that year.</p>
<p>If you don't need the actual entry data it might be more efficient to go a bit more manual - for example using <code>map</code> and <code>unique</code> to loop over everything and store into a new array:</p>
<pre class="lang-twig prettyprint-override"><code>{% set years = craft.entries.section('handle').all()|map(entry =&gt; entry.postDate|date(&quot;Y&quot;))|unique %}

&lt;ol&gt;
    {% for year in years %}
        &lt;li&gt;{{ year }}&lt;/li&gt;
    {% endfor %}
&lt;/ol&gt;
</code></pre>
<p><a href="https://twig.symfony.com/doc/3.x/filters/map.html" rel="nofollow noreferrer">https://twig.symfony.com/doc/3.x/filters/map.html</a></p>
<p>If you're not comfortable with <code>map</code> you could do the same thing with a more lengthy combination of a <code>for</code> loop and the <code>merge</code> or <code>push</code> filters to push all the years into a new array before filtering them with <code>unique</code>, or using conditionals to only add the year if it hasn't already been added.</p>
<p><a href="https://craftcms.com/docs/4.x/dev/filters.html#unique" rel="nofollow noreferrer">https://craftcms.com/docs/4.x/dev/filters.html#unique</a></p>
<p><a href="https://craftcms.com/docs/4.x/dev/filters.html#push" rel="nofollow noreferrer">https://craftcms.com/docs/4.x/dev/filters.html#push</a></p>

CC BY-SA 4.0
2024-09-02T08:12:26.373Z
2024-09-02T08:12:26.373Z
James Smith
<p>Your question is a bit confusing.
But, I suppose you are looking for something like that:</p>
<pre><code>{% set entries = craft.entries.section('yourSectionHandle').all() %}
{% for entry in entries %}
  {{ entry.postDate|date('Y') }}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2024-09-01T14:36:31.347Z
no, it will just go through all the elements and display the year from each element. I also need to get a list of the years for which there are items.
Dimi
CC BY-SA 4.0
2024-08-30T22:16:12.977Z
2024-08-30T22:16:12.977Z
RomanAvr
<p>I have a section with entries. I only need to get the years for which there are entries. How to do it?</p>

<craft3><entries><craft4>
How can I display a list of all the years for which there are entries?
# How can I display a list of all the years for which there are entries?

(Asked by: username_0 on 2024-08-30)

I have a section with entries. I only need to get the years for which there are entries. How to do it?

**Tags:** craft3, entries, craft4


---

## 2 answers

### Answer by username_1 on 2024-09-02. Score: 3 | Accepted answer

There are lots of different ways to do this. Have you tried anything so far?
You could use the group filter to group all the entries by their postDate year:
{% set entriesGroupedByYear = craft.entries.section('handle').all()|group(entry => entry.postDate|date("Y")) %}


    {% for year, entries in entriesGroupedByYear %}
        {{ year }}
    {% endfor %}



https://craftcms.com/docs/4.x/dev/filters.html#group
This will give you a two-dimensional array where the keys are the years and the values are arrays of entries belonging to that year.
If you don't need the actual entry data it might be more efficient to go a bit more manual - for example using map and unique to loop over everything and store into a new array:
{% set years = craft.entries.section('handle').all()|map(entry => entry.postDate|date("Y"))|unique %}


    {% for year in years %}
        {{ year }}
    {% endfor %}


https://twig.symfony.com/doc/3.x/filters/map.html
If you're not comfortable with map you could do the same thing with a more lengthy combination of a for loop and the merge or push filters to push all the years into a new array before filtering them with unique, or using conditionals to only add the year if it hasn't already been added.
https://craftcms.com/docs/4.x/dev/filters.html#unique
https://craftcms.com/docs/4.x/dev/filters.html#push


---

### Answer by username_2 on 2024-08-30. Score: 1

Your question is a bit confusing.
But, I suppose you are looking for something like that:
{% set entries = craft.entries.section('yourSectionHandle').all() %}
{% for entry in entries %}
  {{ entry.postDate|date('Y') }}
{% endfor %}

> **Comments:**
> **username_0**: no, it will just go through all the elements and display the year from each element. I also need to get a list of the years for which there are items.  

---

