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?

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
a-am
  • 2,857
  • 1
  • 19
  • 25

1 Answers1

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 %}
Lindsey D
  • 23,974
  • 5
  • 53
  • 110
  • You're very welcome! :) – Lindsey D Aug 09 '14 at 03:18
  • Is it possible to only compare to the month itself so that it finds all entries in that month regardless of year? – a-am 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