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>
</div>
{% 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) %}