I want to build an archive template where only entries older than 12 months show up. Is there an clean and easy way to do so? I only have strange calculations in mind. Something like this would be nice :)

{% for entry in craft.entries.section('news') if entry.postDate > 12monthsago %}
{% endfor %}

You were close, you can set a postDate param on your craft.entries criteria model. And the Twig filter date_modify helps you to get the DateTime from 12 months ago.

{% set timeAgo = now|date_modify('5:00 -12 months') %}
{% set postDateParam = '< ' ~ timeAgo|date('U') %}

{% set entries = craft.entries.section('news').postDate(postDateParam) %}
|improve this answer|||||
  • Just to understand your code: {% set timeAgo = now|date_modify('5:00 -12 months') %} sets timeAgo to now(5 o'clock) - 12 months or what does the 5:00 stand for? – Rattikarl Feb 18 '17 at 23:37
  • 1
    Yes, exactly. Set the time to 5 a.m. then go back 12 months. This makes sure that the page changes once per day (at 5 a.m.). See PHP docs for all the possible relative time formats php.net/manual/en/datetime.formats.relative.php – carlcs Feb 19 '17 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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