Better Events Listings
Problem
A simple filter by date in the original query so you don’t have to loop through all the entries and manually filter out upcoming events.
Solution
The simplest way, with events with just a single custom date time field called dateTime
.
{% set events = craft.entries.find({section:'events', dateTime : ">= " ~ now.w3c() , order:'dateTime'}) %}
{% if events|length %}
{% for event in events %}
<article>
<h2>{{ event.title }}</h2>
{# any other custom info about the event goes here #}
</article>
{% endfor %}
{% endif %}
A more complicated example for multi-day events and different views. (assuming the section is called “exhibitions”
Setup two custom routes in the backend for:
- exhibitions/future loads exhibitions.html template
- exhibitions/past loads exhibitions.html template
Setup the params based on which url where “exhibitions/” pulls all current exhibitions “exhibitions/future” pulls ones that haven’t opened yet, and “exhibitions/past” are all the ones that have closed.
{% if craft.request.getSegment(2) == 'past' %}
{% set params = { section: 'exhibitions', startDate: "<= " ~ now , endDate: "<= " ~ now , order:'startDate'} %}
{% elseif craft.request.getSegment(2) == 'future' %}
{% set params = { section: 'exhibitions', startDate: "> " ~ now, order:'startDate'} %}
{% else %}
{% set params = { section: 'exhibitions', startDate : "<= " ~ now , endDate: ">= " ~ now , order:'startDate' } %}
{% endif %}
{% set exhibitions = craft.entries.find(params) %}
{% if exhibitions|length %}
{% for exhibition in exhibitions %}
<article>
<h2>{{ exhibition.title }}</h2>
{# any other custom info about the exhibition goes here #}
</article>
{% endfor %}
{% endif %}
That’s it!
Submitted by Keith Mancuso on 1st February, 2017