Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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 %}
share|improve this answer
    
Thanks Lindsey! –  aran Aug 9 at 3:17
    
You're very welcome! :) –  Lindsey D Aug 9 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 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 at 16:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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