<p>You can do this using the Lowblocks plugin:</p>

<p><a href="https://github.com/low/lowblocks" rel="nofollow noreferrer">https://github.com/low/lowblocks</a></p>

<p>Im using it for exactly the same thing.</p>

<p><a href="https://craftcms.stackexchange.com/questions/39/search-matrix-for-blocks-with-upcoming-dates/41#comment5242_41">Brandon Kelly has said</a> he's working on an enhancement in 2.3 to allow filtering Matrix fields by date.</p>

<p>In the meantime I used this temporary workaround...</p>

<pre><code>{% set allDates = craft.lowblocks.blocks.fieldId(136).type('mbDate').order('mfDate').limit(null) %}

{% set upcomingIds = [] %}

{% for item in allDates if item.mfDate &gt; now %}

    {% set upcomingIds = upcomingIds | merge([item.id]) %}

{% endfor %}

{% set upcomingDates = craft.lowblocks.blocks.id(upcomingIds).fieldId(136).type('mbDate').order('mfDate').limit(30) %}
</code></pre>

CC BY-SA 3.0
2014-11-14T11:08:15.633Z
The plugin seems to be exactly what I need! I have only one problem using it: I'd like to query only blocks with a future `date` value. I tried using `craft.lowblocks.blocks.fieldId(12).date('>= NOW()')` but I get a _Craft\ElementCriteriaModel and its behaviors do not have a method or closure named "date"_ exception. Ordering block using the date field works however: `craft.lowblocks.blocks.fieldId(12).order('date DESC')`. Can you give me an example how you did it?
michaelhue
CC BY-SA 3.0
2014-11-15T12:30:30.543Z
Sorry I forgot about that bit. I've edited my answer to include a link to a comment by BK who's said he's working on this issue for 2.3, and also a temporary workaround I used. Hope it helps.
Jamie Pittock
CC BY-SA 3.0
2014-11-17T08:37:52.867Z
Thanks for the workaround! Looking forward to the updated version of the Lowblocks plugin but for now, this is the best answer to my problem. Thank you Jamie!
michaelhue
CC BY-SA 3.0
2014-12-04T19:03:47.573Z
Just wanted to note that with version 2.3 of Craft this workaround is no longer necessary.

> It is now possible to set custom field parameters on Matrix block queries.
michaelhue
CC BY-SA 3.0
2014-11-12T17:42:31.267Z
2014-11-15T12:51:23.533Z
2017-04-13T12:51:00.303Z
Jamie Pittock
<p>If the native solutions seem a bit involved, or if you want some additional calendar processing power for your events <em>(recurring events, calendar display, etc.)</em>, there's also <strong>Craft Calendars</strong>: <a href="http://michaelrog.com/craft/calendars" rel="nofollow">http://michaelrog.com/craft/calendars</a></p>

CC BY-SA 3.0
2014-11-23T17:16:36.863Z
Thanks for the suggestion Michael! Your plugin seems very useful but I think it's a bit overkill for my purposes.
michaelhue
CC BY-SA 3.0
2015-08-16T00:31:57.033Z
Very interested to test your plugin Michael. Tell me if still possible to test please?
migmaker
CC BY-SA 3.0
2015-08-20T16:31:18.563Z
Sure — support@topshelfcraft.com
Michael Rog
CC BY-SA 3.0
2014-11-18T09:43:56.273Z
2014-11-18T09:43:56.273Z
Michael Rog
<p>I think that in order to do this you are going to need to loop through all events; find eventTimes (within the blocks) that are after the selected date; merge those into an array;  and then return the sorted array. However, by doing so you will loose the ability to easily use pagination. </p>

<p>Perhaps another solution would be to not use matrix blocks at all and instead create two channels: 1 for 'Shows' and 1 for 'Showtimes' (using your theatre analogy), where 'Showtimes' would be related to 'Shows' using an Entries field-type with limit set to 1. Then finding showtimes would be trivial, and would support pagination, etc. (You could even set the showtimes title to be auto-generated from 'related show title + datetime'.)</p>

<p>And if you wanted to display all the showtimes collected nicely on the Shows entry (for the sake of the client), you can use a nice plugin called '<a href="https://github.com/iainurquhart/craft_introvert" rel="nofollow">introvert</a>' to show reverse related entries.</p>

CC BY-SA 3.0
2014-11-14T11:02:42.657Z
Thanks for your suggestions! Looping through all events  and replicating the pagination behavior is a last resort for me. Two channels would be nice from a data normalizing view but I'm afraid it will be too much of a hassle for the authors. Introvert looks useful, I will keep it in mind! Thanks again!
michaelhue
CC BY-SA 3.0
2014-11-12T15:40:44.633Z
2014-11-12T15:40:44.633Z
Douglas McDonald
<p>I have a section <code>Events</code> with a matrix field (let's call it <code>dates</code>). The matrix features a single block type with a single <code>date</code> field. One <code>Event</code> can have multiple dates. Think theatre plays with multiple show dates.</p>

<p>Now I'd like to create a list of all events, ordered by the <code>date</code> field. This means that the same entry could show multiple times in the list (once for each <code>date</code> in the matrix field). I want to query all future dates in the matrix, fetch related entries, limit the number of items displayed and support pagination.</p>

<p>Ideally there is something like <code>craft.entries</code> but for matrix fields. </p>

<h3>Example</h3>

<p>The following example shows three different <code>Event</code> entries, ordered by their <code>date</code>s. Notice how the same entry can appear multiple times.</p>

<ol>
<li>14/12/01 @8pm - The Great Gatsby (ID #1)</li>
<li>14/12/01 @8pm - Moby Dick (ID #2)</li>
<li>14/12/02 @3pm - Snow White (ID #3)</li>
<li>14/12/02 @5pm - Snow White (ID #3)</li>
<li>15/12/05 @7pm - The Great Gatsby (ID #1)</li>
</ol>

<hr>

<p>Can anyone point me in the right direction? I'm not afraid to dive into plugin development (already created a simple "Address" field type with geocoding support) but is there an easier way? Thanks for your time!</p>

CC BY-SA 3.0
2014-11-12T13:06:56.793Z
Thanks @JoshAngell for the spelling corrections. English is not my first language as you might be able to tell. :)
michaelhue
CC BY-SA 3.0
2014-11-12T13:08:08.607Z
No it is very well written - in fact that was why I edited it :)
Josh Angell
CC BY-SA 3.0
2014-11-12T15:56:15.433Z
@michaelhue To be fair, it's only "theatre" in the U.K. I would have left it as "theater". :)
Brad Bell
CC BY-SA 3.0
2014-11-15T12:43:26.330Z
@BradBell OK WE'RE WEIRD.
Josh Angell
<matrix>
Query entries by matrix date fields for event calendar
# Query entries by matrix date fields for event calendar

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

I have a section `Events` with a matrix field (let's call it `dates`). The matrix features a single block type with a single `date` field. One `Event` can have multiple dates. Think theatre plays with multiple show dates.

Now I'd like to create a list of all events, ordered by the `date` field. This means that the same entry could show multiple times in the list (once for each `date` in the matrix field). I want to query all future dates in the matrix, fetch related entries, limit the number of items displayed and support pagination.

Ideally there is something like `craft.entries` but for matrix fields.

### Example

The following example shows three different `Event` entries, ordered by their `date`s. Notice how the same entry can appear multiple times.

1. 14/12/01 @8pm - The Great Gatsby (ID #1)
2. 14/12/01 @8pm - Moby Dick (ID #2)
3. 14/12/02 @3pm - Snow White (ID #3)
4. 14/12/02 @5pm - Snow White (ID #3)
5. 15/12/05 @7pm - The Great Gatsby (ID #1)

---

Can anyone point me in the right direction? I'm not afraid to dive into plugin development (already created a simple "Address" field type with geocoding support) but is there an easier way? Thanks for your time!

**Tags:** matrix
> **Comments:**
> **username_0**: Thanks @JoshAngell for the spelling corrections. English is not my first language as you might be able to tell. :)  
> **username_1**: No it is very well written - in fact that was why I edited it :)  
> **username_2**: @michaelhue To be fair, it's only "theatre" in the U.K. I would have left it as "theater". :)  
> **username_1**: @BradBell OK WE'RE WEIRD.  

---

## 3 answers

### Answer by username_3 on 2014-11-12. Score: 1 | Accepted answer

You can do this using the Lowblocks plugin:
https://github.com/low/lowblocks
Im using it for exactly the same thing.
Brandon Kelly has said he's working on an enhancement in 2.3 to allow filtering Matrix fields by date.
In the meantime I used this temporary workaround...
{% set allDates = craft.lowblocks.blocks.fieldId(136).type('mbDate').order('mfDate').limit(null) %}

{% set upcomingIds = [] %}

{% for item in allDates if item.mfDate > now %}

    {% set upcomingIds = upcomingIds | merge([item.id]) %}

{% endfor %}

{% set upcomingDates = craft.lowblocks.blocks.id(upcomingIds).fieldId(136).type('mbDate').order('mfDate').limit(30) %}

> **Comments:**
> **username_0**: The plugin seems to be exactly what I need! I have only one problem using it: I'd like to query only blocks with a future `date` value. I tried using `craft.lowblocks.blocks.fieldId(12).date('>= NOW()')` but I get a _Craft\ElementCriteriaModel and its behaviors do not have a method or closure named "date"_ exception. Ordering block using the date field works however: `craft.lowblocks.blocks.fieldId(12).order('date DESC')`. Can you give me an example how you did it?  
> **username_3**: Sorry I forgot about that bit. I've edited my answer to include a link to a comment by BK who's said he's working on this issue for 2.3, and also a temporary workaround I used. Hope it helps.  
> **username_0**: Thanks for the workaround! Looking forward to the updated version of the Lowblocks plugin but for now, this is the best answer to my problem. Thank you Jamie!  
> **username_0**: Just wanted to note that with version 2.3 of Craft this workaround is no longer necessary.  > It is now possible to set custom field parameters on Matrix block queries.  

---

### Answer by username_4 on 2014-11-18. Score: 1

If the native solutions seem a bit involved, or if you want some additional calendar processing power for your events *(recurring events, calendar display, etc.)*, there's also **Craft Calendars**: <http://michaelrog.com/craft/calendars>

> **Comments:**
> **username_0**: Thanks for the suggestion Michael! Your plugin seems very useful but I think it's a bit overkill for my purposes.  
> **username_5**: Very interested to test your plugin Michael. Tell me if still possible to test please?  
> **username_4**: Sure — support@topshelfcraft.com  

---

### Answer by username_6 on 2014-11-12. Score: 1

I think that in order to do this you are going to need to loop through all events; find eventTimes (within the blocks) that are after the selected date; merge those into an array; and then return the sorted array. However, by doing so you will loose the ability to easily use pagination.

Perhaps another solution would be to not use matrix blocks at all and instead create two channels: 1 for 'Shows' and 1 for 'Showtimes' (using your theatre analogy), where 'Showtimes' would be related to 'Shows' using an Entries field-type with limit set to 1. Then finding showtimes would be trivial, and would support pagination, etc. (You could even set the showtimes title to be auto-generated from 'related show title + datetime'.)

And if you wanted to display all the showtimes collected nicely on the Shows entry (for the sake of the client), you can use a nice plugin called '[introvert](https://github.com/iainurquhart/craft_introvert)' to show reverse related entries.

> **Comments:**
> **username_0**: Thanks for your suggestions! Looping through all events  and replicating the pagination behavior is a last resort for me. Two channels would be nice from a data normalizing view but I'm afraid it will be too much of a hassle for the authors. Introvert looks useful, I will keep it in mind! Thanks again!  

---

