2

I'm trying to use Supersort to sort entries using a date/time field within a matrix, for example 1/6/2019 12:00AM. These dates will always be in the future. I want to sort it so that the entries that are closest to happening are displayed first.

To make it more complicated, entries with multiple dates in the matrix are split apart, so I need to sort each of these according to their date (from https://gocrowsnest.ca/events):

https://gocrowsnest.ca/events

I can't figure out how exactly I'm supposed to do this. What I have:

{% set pageEntries = craft.entries.section('events') | supersort('sortAs', '{dates.type("date").first.startDate.getTimestamp}') %}
{% for event in pageEntries %}
    {% for block in event.dates.all() %}
        {# Event code #}
    {% endfor %}
{% endfor %}

This is sorting each entry by the first matrix date, not by each date.

Matrix handle: dates, block: date, field (date/time): startDate.

  • 1
    Can you elaborate on what you mean by sorting by a field in a matrix block? Presumably the matrix date field has a date in it... but how are you wanting to use that to sort entries in a channel? If I have 2018.11.25 in a date field, how would this apply to sorting entries? – andrew.welch Nov 24 '18 at 22:42
  • Updated with more context. – Bryce Nov 26 '18 at 2:40
1

So you're not getting your matrix field's value properly. You're going to want to access it the same way you'd output the data:

{{ event.dates.type('date').first().startDate.getTimestamp() }}

So what we're doing here is getting the dates matrix field, selecting the blocks that have a type of date, getting the first of those blocks (should be our only block but .first is still needed), then get the startDate field in that block. During my testing I found you needed something like .getTimestamp() or .atom() to turn the date into a string that SuperSort could understand (not sure why the examples in our docs don't use that, will definitely look into that though).

So something like this should achieve what you're looking for:

{% set pageEntries = craft.entries.section('events') | supersort('sortAs', '{dates.type("date").first.startDate.getTimestamp}') %}
{% for event in pageEntries %}
    {# event data #}
{% endfor %}

Hopefully that helps!

Fwiw, I work for TopShelfCraft (who made the SuperSort plugin)

  • Thanks! Seems like that's doing the trick for the problem I described, but my issue is a bit more complicated than I initially thought. I've edited the original question. Really appreciate your help! – Bryce Nov 27 '18 at 3:12

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.