<p>I got it using the 'group' filter at : <a href="http://buildwithcraft.com/docs/templating/filters#group" rel="nofollow">http://buildwithcraft.com/docs/templating/filters#group</a></p>

<p>Thanks to Steve Abraham for pointing me in the right direction. My code that seems to work is:</p>

<pre class="lang-twig prettyprint-override"><code>{% set companyHistory = craft.entries.section('about').slug('company-history').first() %}
{% set allEntriesByYear = companyHistory.children.type('history') | group('date.year') %}

{% for year, entriesInYear in allEntriesByYear %}
    &lt;h2&gt;{{ year }}&lt;/h2&gt;
    {% for moment in entriesInYear %}
        &lt;li&gt;{{ moment.title }}&lt;/li&gt;
    {% endfor %}
{% endfor %}
</code></pre>

<p>Let me know if there is an even better solution. </p>

CC BY-SA 3.0
2014-07-07T14:23:55.437Z
Superb, and I didn't even have to answer. ;)
Steve Abraham
CC BY-SA 3.0
2014-07-07T14:22:31.187Z
2014-07-08T22:03:14.733Z
2014-07-08T22:03:14.733Z
Christopher Healey
<p>I'm outputting a timeline for company history and i'd like to add a graphic for the Year before the first entry of each year. My current code is as follows:</p>

<pre class="lang-twig prettyprint-override"><code>{% set companyHistory = craft.entries.section('about').slug('company-history').first() %}

{% for moment in companyHistory.children.type('history') %}
    &lt;li&gt;{{ moment.title }}&lt;/li&gt;
{% endfor %}
</code></pre>

<p>My date field is simply called 'date'. How might I go about this with the fewest possible DB queries?</p>

<entries><date>
Do something if first entry of a year
# Do something if first entry of a year

(Asked by: username_0 on 2014-07-07)

I'm outputting a timeline for company history and i'd like to add a graphic for the Year before the first entry of each year. My current code is as follows:
{% set companyHistory = craft.entries.section('about').slug('company-history').first() %}

{% for moment in companyHistory.children.type('history') %}
    {{ moment.title }}
{% endfor %}

My date field is simply called 'date'. How might I go about this with the fewest possible DB queries?

**Tags:** entries, date


---

## 1 answers

### Answer by username_0 on 2014-07-07. Score: 3 | Accepted answer

I got it using the 'group' filter at : http://buildwithcraft.com/docs/templating/filters#group
Thanks to username_1 for pointing me in the right direction. My code that seems to work is:
{% set companyHistory = craft.entries.section('about').slug('company-history').first() %}
{% set allEntriesByYear = companyHistory.children.type('history') | group('date.year') %}

{% for year, entriesInYear in allEntriesByYear %}
    {{ year }}
    {% for moment in entriesInYear %}
        {{ moment.title }}
    {% endfor %}
{% endfor %}

Let me know if there is an even better solution.

> **Comments:**
> **username_1**: Superb, and I didn't even have to answer. ;)  

---

