I'm trying to make a archive page for a blog sorted by months. I tried to consult the solution from Linking and showing archive by month, but I can't get it to work like it should. The code I have now just displays all entries regardless of their post date.

{% set catch = year ~ '-' ~ month ~ '-01' %}
{% set firstDay = catch | date_modify('first day of ', month ~ year ~ ' 00:00:00') %}
{% set lastDay = catch | date_modify('last day of ', month ~ year ~ ' 23:59:59') %}
{% set archive  = craft.entries.section('blog').postDate('and', '>= ' ~ firstDay.date, '<= ' ~ lastDay.date) %}
{% for entry in archive %}
    <div class="posts-block">
        <h3>{{ entry.title }}</h3>
{% endfor %}

I had to add ".date" behind "firstDay" and "lastDay", or I get an error (Object of class DateTime could not be converted to string) If I remove the "and" from "archive" like this:

{% set archive  = craft.entries.section('blog').postDate('>= ' ~ firstDay.date, '<= ' ~ lastDay.date) %}

then it sorts my entries according to "firstDay", but completely ignores "lastDay". I tried to threw an and into the middle of the string, but that didn't to anything.

I also tried to use .after and .before, but then I just get what seems like a random set of entries...

{% set archive = craft.entries.section('blog').limit(null).after(firstDay).before(lastDay) %}

1 Answer 1


The value you need to set the postDate parameter to is quite complex. If you define a dedicated variable for it, it makes the code more readable and you can more easily debug the value using the dump() function.

For the problem itself, you were on the right track. The main problem is a syntax mistake, you are passing in the 'and', '>= ', ... values without wrapping them as an array.

Some other changes I’d suggest is to use the < operator in combination with a relative time of 'first day of next month', so every entry before 00:00 of that day (see documentation of relative date/time formats).

And instead of the .date property, I'd rather use the date filter to output the DateTime object as a string in the right format. Which can be 'Y-m-d' to omit the time completely (Craft sets it back to 00:00), or one of the characters that output a full date with time 'c' or 'U' (see documentation of date format parameter).

{% set firstDay = date(year ~ '-' ~ month ~ '-01') %}
{% set lastDay = firstDay|date_modify('first day of next month') %}

{% set postDateParam = ['and', '>= ' ~ firstDay|date('c'), '< ' ~ lastDay|date('c')] %}

<pre>{{ dump(postDateParam) }}</pre>

{% set archive = craft.entries({
    section: 'blog',
    postDate: postDateParam,
}) %}
  • This works and was easy to understand, thank you so much!
    – Maria
    Commented Oct 5, 2017 at 13:45
  • I had the same doubt. Thank you very much. Commented Oct 5, 2017 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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