4

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 %}
Brad Bell
  • 67,440
  • 6
  • 73
  • 143
Rattikarl
  • 393
  • 2
  • 9

1 Answers1

7

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) %}
carlcs
  • 36,220
  • 5
  • 62
  • 139
  • 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 http://php.net/manual/en/datetime.formats.relative.php – carlcs Feb 19 '17 at 11:07