2

Here is how I am filtering entries by a specific month:

{% set filterId = "July" %}
{% set allEntries = craft.entries.section('messages') %}
{% set filteredEntries = [] %}

{% for msg in allEntries %}

  {% if msg.messageDate | date('F') == filterId | date('F') %}
    {% set filteredEntries = filteredEntries | merge([msg]) %}
  {% endif %}

{% endfor %}

{% set messageList = filteredEntries %}

How can I paginate messageList? Do I need to filter by month a different way for pagination to work?

3

You won't be able to paginate messageList, because that's just an array... According to the official documentation on the {% paginate %} tag, only an ElementCriteriaModel can be paginated.

To see how to create an ElementCriteriaModel which has already been filtered by month, check out Solution #2 in this thread...

You'll end up with code that looks something like this:

{% set firstDayOfMonth = year ~ "-" ~ month ~ "-01" %}
{% set daysInThisMonth = firstDayOfMonth|date('t') %}
{% set lastDayOfMonth  = year ~ "-" ~ month ~ "-" ~ daysInThisMonth %}

{% set thisMonthsEntries = craft.entries.section('messages').messageDate('and', '>= ' ~ firstDayOfMonth, '<= ' ~ lastDayOfMonth) %}

{% paginate thisMonthsEntries as entriesOnPage %}
    {% for entry in entriesOnPage %}
        {# ... whatever your code does... #}
    {% endfor %}
{% endpaginate %}
  • You're very welcome! :) – Lindsey D Aug 9 '14 at 3:18
  • Is it possible to only compare to the month itself so that it finds all entries in that month regardless of year? – aran Aug 11 '14 at 16:19
  • The code to pull that off would be very different from what we've got here... This example creates a date range (from the 1st - 31st of a given month). What you just described would actually look for multiple ranges across X years. Since the pagination portion of this question has already been answered, I'd suggest posing that as a fresh question. – Lindsey D Aug 11 '14 at 16:31

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.