I want to list all entries in a section by custom date time fields.

A training course has a From field and a To field. These fields are inside a Matrix field.

The field name storing this info is called trainingDateTime.

The Matrix block is called trainingDateTimeDateTime.

The Matrix fields in this block are called and dateTimeFrom and dateTimeTo both datetime fields.

My entries loop so far is:

{% set entries = craft.entries.section("training") %}

I know you can do order entries by a custom datetime field using something like:

{% set entries = craft.entries.section("training").customDateTime.('>= ' ~ now).order('customDateTime DESC') %}

But since I'm using a matrix field I'm unsure how to approach this.

I'm wanting to show all upcoming training courses from the current month up till the next 2 months.

up vote 2 down vote accepted

I found the solution by using the following:

{% set startDate = now %}
{% set endDate = (now | date_modify("+2 months")) %}
{% set allTraining = craft.lowblocks.blocks.fieldId(72).type("trainingDateTimeDateTime").order("dateTimeFrom").limit(null) %}
{% set entryIds = [] %}
{% for course in allTraining %}
    {% if (course.dateTimeFrom >= startDate) and (course.dateTimeFrom <= endDate) %}
        {% set entry = course.owner %}
        {% set entryIds = entryIds | merge([entry.id]) %}
    {% endif %}
{% endfor %}
{% set entries = craft.entries.section("training").id(entryIds).fixedOrder(true).status("live,pending") %}
  • Is there any way in Craft 2.3 that this is doable without the use of a plugin? I have a matrix field with a date in it and I want to pull out the last 3 entries. Almost exactly what you have, but native. – Aaron Mar 9 '15 at 22:40

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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