I have a matrix field with one "event" block type that contains several fields. One of those fields is a date field. Is there a way to get just blocks with upcoming dates?


Here’s how you would do that:

{% set futureEvents = entry.myMatrixField.myDateField('>= '~now) %}

{% for event in futureEvents %}
    {{ event.description }}
{% endfor %}


This actually hadn’t worked as I expected to, but it does as of Craft 2.3.

| improve this answer | |
  • This method is would less of an performance impact than using a conditional to determine whether or not to display the entry in a for loop. – Steve Adams Jun 12 '14 at 10:33
  • 1
    Should this technique work with any matrix field, I have a matrix block which has a field which holds a section ID but {% set pub = globalPubSettings.pubSettings.pubId(entry.section.id) %} is throwing a method not found for pubId? – Sam Hibberd Aug 21 '14 at 15:44
  • I have the same problem as Sam Hibberd, this method throws a CException "no method or closure with the name 'myDateField'". – Florian Sep 5 '14 at 8:44
  • @BrandonKelly Is there a solution for this, just revisting and hoping I can get {% set var = page.matrixField.showOnHome('1') %} to work. Still throwing the same exception. – Sam Hibberd Nov 5 '14 at 14:42
  • 6
    @SamHibberd Search wouldn’t work here, no. After talking the problem over with Brad at the end of the day yesterday, I realized there was a way we could get it working before 3.0 without breaking existing element types (or at least making them any more broken then they already are in this case). Spent the day working on this, and am happy to report I’ve just gotten it all working for 2.3. – Brandon Kelly Nov 7 '14 at 0:54

Would this do it?

{% set blocks = entry.matrixFieldHandle %}
{% for block in blocks if block.yourDateField|date >= now|date %}

{% endfor %}
| improve this answer | |

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.