Is there a way to order my forloop by a custom field in my Blueprint. I see you can order by date but if I have a custom Date field, can I use that instead?
for example:
Blueprint
header.overview.datestart:
type: datetime
label: Start Date #PLUGIN_ADMIN.DATE
toggleable: true
Front End
{% set this_year = "now"|date('M d Y') %}
{% set event_date = feature.header.overview.datestart|date('M d Y') %}
{% for feature in page.find('/events').children().order('event_date').slice(0, 3) %}
{{ event_date }}
<li>
{{ feature.title }}
{{ feature.header.overview.datestart|date("M d Y") }}
</li>
{% endfor %}
Hmm, I don’t think this will get the results I’m looking for @paul unfortunately.
I need the comparison to be based off the custom date field and not the default date field. Right now your example results to nothing outputting. The feature.header.overview.datestart needs to be in the equation some type of way i believe.
These are the Datefields I’m using for my events. There is a Date Start and a Date End.
columns:
type: columns
fields:
column.start:
type: column
fields:
header.overview.datestart:
type: datetime
label: Start Date #PLUGIN_ADMIN.DATE
toggleable: true
help: PLUGIN_ADMIN.DATE_HELP
style: vertical
size: medium
column.end:
type: column
fields:
header.overview.dateend:
type: datetime
label: End Date #PLUGIN_ADMIN.DATE
toggleable: true
help: PLUGIN_ADMIN.DATE_HELP
style: vertical
size: medium
On the front end I want to Pull the latest 3 Events based on Today’s Date – the Now. That way when one event ends, the next event will populate this loop – So the latest 3 events will always show. Is there a way to do this without doing something really hackie?
well, then you slice filter si not at its place.
First step would be to get only future date, and second step would be to slice, or use anoher condition.
Regarding date, you can juste change page.date to page.header.yourcustomdate
Edited example:
{% set this_year = "now"|date('Ymd') %}
{% for feature in page.find('/events').children() if feature.header.datestart|date('Ymd') > this_year %}
{% if loop.index < 4 %}
<li>
{{ feature.title }}
{{ feature.header.overview.datestart|date("M d Y") }}
</li>
{% endif %}
{% endfor %}
@paul hold on, I can change the page.date to whatever? how so? That may be the easiest solution as I’m not using this template for a blog or to display a creation/publish date.
Also, I was thinking something similar that my slice might be in the wrong place. Your example however outputs the date in the default page order. It seems like the loop is ignoring the if statement or ignoring the custom field entirely and spitting out the data of the children pages, and constraining the data to just 3 events. Its basically doing everything but ordering it. However, if I can just replace the page.date, that may be the solution I need.
I tried setting the page.date to header.overview.datestart but that produces an error. I’m guessing this is a syntax error where you cant use a “.” for the variable.
{% set page.date = header.overview.datestart %}
But if I can replace the default date to the header.overview.datestart date, then that may resolve everything.
isn’t the .order part of this loop simply using page.date?
I mean I can reorder the pages manually in the admin and get the results I need but I’d hate to do this whenever an event starts/ends. Right now, the header.datestart and header.dateend fields I’m using to display the dates of the events. But I would like to use the .datestart field to order the events by the date stored in this field and not by the default date (created/modified). I guess its not possible to order these child pages by the .datestart field.
but the answer is yes, you can replace with your field, such as
.order(‘header.datestart’, desc) and it should work.
If your page selection is that complex, you should instead use page collections, this would give you more control would you need to setup this differently in the future, and would be easier to use.
I’m sorry I’m beating this post into a grave, I’m just trying to understand. So I’m looking into page.collections. Is the collection only established at the top of the page or could I do something like this:
columns:
type: columns
fields:
column.start:
type: column
fields:
header.overview.datestart:
type: datetime
label: Start Date #PLUGIN_ADMIN.DATE
toggleable: true
help: PLUGIN_ADMIN.DATE_HELP
style: vertical
size: medium
content:
items:
'@page': '/events'
order:
by: date
I haven’t seen many examples/recipes for collections.