0

I am trying to sort the output of a forloop in Grav CMS by a custom field from my template Blueprint. I have an event page with child pages of the actual events. I'm trying to loop through the child pages and sort the output by a custom field that uses a date. In a nutshell, show the latest events sorted by date. Currently my loop only outputs by page order and not by the custom date field.

Blueprint (custom date field)

header.overview.datestart:
  type: datetime
  label: Start Date #PLUGIN_ADMIN.DATE
  toggleable: true

Front-End

{% set this_year = "now"|date('M d Y') %}       
        {% for feature in page.find('/events').children().order('feature.header.overview.datestart').slice(0, 3) %}

    <li>
       {{ feature.title }}
       {{ feature.header.overview.datestart|date("M d Y") }}
    </li>

{% endfor %}

The above code outputs the correct data but not in the order I need. Any help would be appreciated.

Cedric W
  • 13
  • 5

1 Answers1

0

As answered in Grav's discourse forum:

You could instead store your date as YYYY-MM-DD to make comparison easier. Then you have to adjust your if to if feature.date|date(‘Y-m-d’) Example:

{% set this_year = "now"|date('Y-m-d') %}   
{% for feature in page.find('/events').children().slice(0, 3) if feature.date|date('Y-m-d') > this_year %}

        <li>
        {{ feature.title }}
        {{ feature.header.overview.datestart|date("M d Y") }}
        </li>

    {% endfor %}
Paul Massendari
  • 417
  • 3
  • 6