<p>I think you are right and there's no chance to do what you want with parameters on a single ElementCriteriaModel.</p>

<p>You could definitely do it with many queries, the first one to get your years array, then one for each year. Merging them is simple with Twig's <a href="http://twig.sensiolabs.org/doc/filters/merge.html" rel="nofollow"><code>merge</code></a> filter. But then you'd end up with a manually composed array of entry models and don't have the convenience of a single ElementCriteriaModel. Methods like <a href="http://buildwithcraft.com/docs/templating/entrymodel#getNext" rel="nofollow"><code>getNext()</code></a> or – important in your case – the <a href="http://buildwithcraft.com/docs/templating/paginate" rel="nofollow"><code>{% paginate %}</code></a> tag won't work with an array of entry models!</p>

<p>Let's skip the paginate problem for now and look at that query flood, you'd produce. Sure, Craft has the <a href="http://buildwithcraft.com/docs/templating/cache" rel="nofollow"><code>{% cache %}</code></a> tag, but if your entries are modified regularly, there'll always be people who access the uncached version.</p>

<p>But what about another solution, why not do a single query to get all your "messages" entries and filter them in your template with a simple conditional:</p>

<pre><code>{# Get all `messages` entries #}
{% set entries = craft.entries.section('messages') %}

{# Loop through all entries with a `messageDate` month of `myMonth` regardless the year #}
{% for entry in entries if entry.messageDate|date('m') == myMonth %}
    {{ entry.title }}
{% endfor %}
</code></pre>

<p>You'd still have no solution yet for your pagination problem, but this should be way less expensive on performance, and it's really easy to implement.</p>

CC BY-SA 3.0
2014-08-12T18:30:09.373Z
I will need pagination due to the amount of entries that will be on the page.  Which makes me think, if there is a lot of overhead to filter the way I need, my UX for filtering might need to change. I may need to have the client offer a drop-down for year as well as month.
a-am
CC BY-SA 3.0
2014-08-12T18:51:03.443Z
You mean you're backpedaling to `.messageDate('and', '>= ' ~ firstDayOfMonth, '<= ' ~ lastDayOfMonth)`?
carlcs
CC BY-SA 3.0
2014-08-12T18:55:15.600Z
Using a single ElementCriteriaModel is probably your best bet to get pagination implemented. All other methods would require a custom replication..
carlcs
CC BY-SA 3.0
2014-08-12T17:23:29.020Z
2014-08-12T17:23:29.020Z
carlcs
<p>I am looking to filter entries by a given month regardless of year. The filter would be based on a custom Date/Time input.  My last goal would allow them to be paginated.</p>

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

{% set thisMonthsEntries = craft.entries.section('messages').messageDate('and', '&gt;= ' ~ firstDayOfMonth, '&lt;= ' ~ lastDayOfMonth) %}

{% paginate thisMonthsEntries as entriesOnPage %}
    {% for entry in entriesOnPage %}
        {# ... whatever your code does... #}
    {% endfor %}
{% endpaginate %}
</code></pre>

<p>This is my original code. My first guess is I would need to have an array of years and loop through each year applying to the above code, however I am unclear on how to get a unique list of years that my entries are related too. Then I would probably have multiple sets of entries which would then need to be merged some how for output.</p>

<entries><pagination><filter>
Filter entries by month regardless of year using custom Date/Time and paginate them
# Filter entries by month regardless of year using custom Date/Time and paginate them

(Asked by: username_0 on 2014-08-12)

I am looking to filter entries by a given month regardless of year. The filter would be based on a custom Date/Time input.  My last goal would allow them to be paginated.
{% 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 %}

This is my original code. My first guess is I would need to have an array of years and loop through each year applying to the above code, however I am unclear on how to get a unique list of years that my entries are related too. Then I would probably have multiple sets of entries which would then need to be merged some how for output.

**Tags:** entries, pagination, filter

---

## 1 answers

### Answer by username_1 on 2014-08-12. Score: 1

I think you are right and there's no chance to do what you want with parameters on a single ElementCriteriaModel.
You could definitely do it with many queries, the first one to get your years array, then one for each year. Merging them is simple with Twig's merge filter. But then you'd end up with a manually composed array of entry models and don't have the convenience of a single ElementCriteriaModel. Methods like getNext() or – important in your case – the {% paginate %} tag won't work with an array of entry models!
Let's skip the paginate problem for now and look at that query flood, you'd produce. Sure, Craft has the {% cache %} tag, but if your entries are modified regularly, there'll always be people who access the uncached version.
But what about another solution, why not do a single query to get all your "messages" entries and filter them in your template with a simple conditional:
{# Get all `messages` entries #}
{% set entries = craft.entries.section('messages') %}

{# Loop through all entries with a `messageDate` month of `myMonth` regardless the year #}
{% for entry in entries if entry.messageDate|date('m') == myMonth %}
    {{ entry.title }}
{% endfor %}

You'd still have no solution yet for your pagination problem, but this should be way less expensive on performance, and it's really easy to implement.

> **Comments:**
> **username_0**: I will need pagination due to the amount of entries that will be on the page.  Which makes me think, if there is a lot of overhead to filter the way I need, my UX for filtering might need to change. I may need to have the client offer a drop-down for year as well as month.  
> **username_1**: You mean you're backpedaling to `.messageDate('and', '>= ' ~ firstDayOfMonth, '<= ' ~ lastDayOfMonth)`?  
> **username_1**: Using a single ElementCriteriaModel is probably your best bet to get pagination implemented. All other methods would require a custom replication..  

---

