Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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') %}
    <li>{{ moment.title }}</li>
{% endfor %}

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

share|improve this question
up vote 3 down vote accepted

I got it using the 'group' filter at :

Thanks to Steve Abraham 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 %}
    <h2>{{ year }}</h2>
    {% for moment in entriesInYear %}
        <li>{{ moment.title }}</li>
    {% endfor %}
{% endfor %}

Let me know if there is an even better solution.

share|improve this answer
Superb, and I didn't even have to answer. ;) – Steve Abraham Jul 7 '14 at 14:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.