Filter entries by month 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', '&gt;= ' ~ firstDay.date, '&lt;= ' ~ lastDay.date) %}
{% for entry in archive %}
    &lt;div class="posts-block"&gt;
        &lt;h3&gt;{{ entry.title }}&lt;/h3&gt;
    &lt;/div&gt;
{% 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('&gt;= ' ~ firstDay.date, '&lt;= ' ~ 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) %}
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', '&gt;= ', ... values without wrapping them as an array.

Some other changes I’d suggest is to use the &lt; 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', '&gt;= ' ~ firstDay|date('c'), '&lt; ' ~ lastDay|date('c')] %}

&lt;pre&gt;{{ dump(postDateParam) }}&lt;/pre&gt;

{% set archive = craft.entries({
    section: 'blog',
    postDate: postDateParam,
}) %}