<p>I've used this method for sorting by year and month (it could be expanded to support days).</p>

<p>On the news/index.html template:</p>

<pre><code>{% if month is defined %} {# show an archive of a month #}

    {% set sortDate = year ~ '-' ~ month %}

    Entry Date: {{ sortDate }}

    {% if month != 12 %}
        {% set nextmonth = month+1 %}
        {% set nextyear = year %}
    {% else %}
        {% set nextmonth = 01 %}
        {% set nextyear = year+1 %}
    {% endif %}

    {% set entriesToShow = craft.entries.section('news').limit(null).after(year ~ '-' ~ month).before(nextyear ~ '-' ~ nextmonth) %}

{% elseif year is defined %} {# show an archive of a year #}

    Entry Date: {{ year }}

    {% set entriesToShow = craft.entries.section('news').limit(null).after(year).before(year+1) %}

{% else %} {# assume overview page #}

    {% set entriesToShow = craft.entries.section('news') %}

{% endif %}
</code></pre>

<p>Any entries can then be displayed by one loop.</p>

<pre><code>{% for entry in entriesToShow %}
</code></pre>

<p>Since this is made to be in on the section's index template you don't need a separate archive template. Just setup a few routes in the CP to send the archive tokens back to the index template.</p>

<p>Set all the token combinations you need like:</p>

<pre><code>news/archive/(month)/(year)
</code></pre>

<p>To route to:</p>

<pre><code>news/index.html
</code></pre>

CC BY-SA 3.0
2014-06-12T21:06:02.810Z
2014-06-12T21:06:02.810Z
Simon Kuran
<p>I ended up getting my archive list working using 'after' and 'before' parameters like below.</p>

<pre><code>{% if year is not defined %}
    {% redirect "news/archives/"~now.year %}
{% endif %}

{% set year  = craft.request.segment(3)  %}

{% set month = craft.request.segment(4) %}

{% set day = craft.request.segment(5) %}

Year: {{ year }}&lt;br&gt;
Month: {{ month }}&lt;br&gt;
Day: {{ day }}&lt;br&gt;&lt;br&gt;

{% if (year | length) and (month | length) and (day | length) %}

    {% set entryAfterDate = year ~ '-' ~ month ~ '-' ~ day %}

    {% set entryBeforeDate = year ~ '-' ~ month ~ '-' ~ (day + 1) %}

{% elseif (year | length) and (month | length) %}

    {% set entryAfterDate = year ~ '-' ~ month %}

    {% set entryBeforeDate = year ~ '-' ~ (month + 1) %}

{% elseif (year | length) %}

    {% set entryAfterDate = year %}

    {% set entryBeforeDate = (year + 1) %}

{% endif %}

After Entry Date: {{ entryAfterDate}}&lt;br&gt;
Before Entry Date: {{ entryBeforeDate}}&lt;br&gt;&lt;br&gt;

{% set entries = craft.entries.find({section: "news", order: "postDate", status: "Live", after: entryAfterDate, before: entryBeforeDate}) %}

{% if entries | length %}

    Entries found.

{% else %}

    No entries found.

{% endif %}
</code></pre>

CC BY-SA 3.0
2014-11-15T22:08:45.607Z
You should better use [`date_modify('+1 month')`](http://twig.sensiolabs.org/doc/filters/date_modify.html) instead of a simple calculations, Sean.
carlcs
CC BY-SA 3.0
2014-06-16T15:53:24.113Z
2014-06-16T15:53:24.113Z
Sean Delaney
<p>I'm trying to create an archive list template that lists entries from a News or Blog Channel based on year/month/day combinations.</p>

<p>However, I get no entries and can't see where I'm going wrong...I'm running the latest build of Craft.</p>

<pre><code>{% if year is not defined %}
    {% redirect "news/archives/"~now.year %}
{% endif %}

{% set year  = craft.request.segment(3)  %}
{% set month = craft.request.segment(4) %}
{% set day = craft.request.segment(5) %}

Year: {{ year }}&lt;br&gt;
Month: {{ month }}&lt;br&gt;
Day: {{ day }}&lt;br&gt;&lt;br&gt;

{% if (year | length) and (month | length) and (day | length) %}
    {% set entryDate = year ~ '-' ~ month ~ '-' ~ day %}
{% elseif (year | length) and (month | length) %}
    {% set entryDate = year ~ '-' ~ month %}
{% elseif (year | length) %}
    {% set entryDate = year %}
{% endif %}

Entry Date: {{ entryDate }}&lt;br&gt;&lt;br&gt;

{% set entries = craft.entries.find({section: "news", postDate: entryDate}) %}

{% if entries | length %}
    Entries found.
{% else %}
    No entries found.
{% endif %}
</code></pre>

CC BY-SA 3.0
2014-06-12T15:33:21.360Z
Just asked you on g+: Where does the code fail? Does it work if you set the entryDate manually?
carlcs
<entries><archives><postdate>
Creating a date filtered archive template
# Creating a date filtered archive template

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

I'm trying to create an archive list template that lists entries from a News or Blog Channel based on year/month/day combinations.
However, I get no entries and can't see where I'm going wrong...I'm running the latest build of Craft.
{% if year is not defined %}
    {% redirect "news/archives/"~now.year %}
{% endif %}

{% set year  = craft.request.segment(3)  %}
{% set month = craft.request.segment(4) %}
{% set day = craft.request.segment(5) %}

Year: {{ year }}
Month: {{ month }}
Day: {{ day }}

{% if (year | length) and (month | length) and (day | length) %}
    {% set entryDate = year ~ '-' ~ month ~ '-' ~ day %}
{% elseif (year | length) and (month | length) %}
    {% set entryDate = year ~ '-' ~ month %}
{% elseif (year | length) %}
    {% set entryDate = year %}
{% endif %}

Entry Date: {{ entryDate }}

{% set entries = craft.entries.find({section: "news", postDate: entryDate}) %}

{% if entries | length %}
    Entries found.
{% else %}
    No entries found.
{% endif %}

**Tags:** entries, archives, postdate
> **Comments:**
> **username_1**: Just asked you on g+: Where does the code fail? Does it work if you set the entryDate manually?  

---

## 2 answers

### Answer by username_2 on 2014-06-12. Score: 5

I've used this method for sorting by year and month (it could be expanded to support days).
On the news/index.html template:
{% if month is defined %} {# show an archive of a month #}

    {% set sortDate = year ~ '-' ~ month %}

    Entry Date: {{ sortDate }}

    {% if month != 12 %}
        {% set nextmonth = month+1 %}
        {% set nextyear = year %}
    {% else %}
        {% set nextmonth = 01 %}
        {% set nextyear = year+1 %}
    {% endif %}

    {% set entriesToShow = craft.entries.section('news').limit(null).after(year ~ '-' ~ month).before(nextyear ~ '-' ~ nextmonth) %}

{% elseif year is defined %} {# show an archive of a year #}

    Entry Date: {{ year }}

    {% set entriesToShow = craft.entries.section('news').limit(null).after(year).before(year+1) %}

{% else %} {# assume overview page #}

    {% set entriesToShow = craft.entries.section('news') %}

{% endif %}

Any entries can then be displayed by one loop.
{% for entry in entriesToShow %}

Since this is made to be in on the section's index template you don't need a separate archive template. Just setup a few routes in the CP to send the archive tokens back to the index template.
Set all the token combinations you need like:
news/archive/(month)/(year)

To route to:
news/index.html


---

### Answer by username_0 on 2014-06-16. Score: 1

I ended up getting my archive list working using 'after' and 'before' parameters like below.
{% if year is not defined %}
    {% redirect "news/archives/"~now.year %}
{% endif %}

{% set year  = craft.request.segment(3)  %}

{% set month = craft.request.segment(4) %}

{% set day = craft.request.segment(5) %}

Year: {{ year }}
Month: {{ month }}
Day: {{ day }}

{% if (year | length) and (month | length) and (day | length) %}

    {% set entryAfterDate = year ~ '-' ~ month ~ '-' ~ day %}

    {% set entryBeforeDate = year ~ '-' ~ month ~ '-' ~ (day + 1) %}

{% elseif (year | length) and (month | length) %}

    {% set entryAfterDate = year ~ '-' ~ month %}

    {% set entryBeforeDate = year ~ '-' ~ (month + 1) %}

{% elseif (year | length) %}

    {% set entryAfterDate = year %}

    {% set entryBeforeDate = (year + 1) %}

{% endif %}

After Entry Date: {{ entryAfterDate}}
Before Entry Date: {{ entryBeforeDate}}

{% set entries = craft.entries.find({section: "news", order: "postDate", status: "Live", after: entryAfterDate, before: entryBeforeDate}) %}

{% if entries | length %}

    Entries found.

{% else %}

    No entries found.

{% endif %}

> **Comments:**
> **username_1**: You should better use [`date_modify('+1 month')`](http://twig.sensiolabs.org/doc/filters/date_modify.html) instead of a simple calculations, Sean.  

---

