2

Two channels: 1) exhibitions, which have a startDate and EndDate 2) events, which have a date

I need to grab the current exhibitions, future exhibitions and future events, which I do using this:

{% set allOccurrences = craft.entries.section('exhibitions').startDate('<= ' ~ now | date("U")).endDate('>= ' ~ now | date("U"))
    | merge(craft.entries.section('exhibitions').startDate('> ' ~ now | date("U")))
    | merge(craft.entries.section('events').date('>= ' ~ now | date("U"))) %}

Now what I need to do is order them, by the startDate (if an exhibition) and the date (if an event). In other words, having already fetched the entries, I need to order them by two different fields.

I'm not sure this is possible without a plugin - could anyone give me any pointers?

3

There's this super useful SuperSort plugin that would make this possible.

{% set sortAsTemplate = '{{ object.section == "Exhibitions" ? object.startDate|date("U") : object.date|date("U") }}' %}
{% set sortedOccurrences = allOccurrences|supersort('sortAs', sortAsTemplate) %}
  • Any idea why this returns "Unexpected "allOccurrences" tag"? – Clive Portman Apr 22 '16 at 10:09
  • Removed the error using: {% set sortedOccurrences = allOccurrences | supersort('sortAs', sortAsTemplate) %} – Clive Portman Apr 22 '16 at 10:16
  • Wasn't sorting properly, though, but this fixed it: {% set sortAsTemplate = '{{ object.startDate | length ? object.startDate|date("U") : object.date|date("U") }}' %} – Clive Portman Apr 22 '16 at 10:16

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.