Query collection by Date Range start/end (E.g future entries)

I'm using the date range field to store event start/end dates.

On a page I want to split up the results into three blocks:

  • Future Events
  • Ongoing Events
  • Past events

So I need to be able to filter the collection by the start and end date in the range field.

The doc's don't seem to mention anything about this though so I'm hitting a bit of a brick wall. Here's the rough code I've got at the moment for testing this:

        <h2>Future Events</h2>
        {{ collection:events start_date:is_before=now end_date:is_after=now }}
            {{ title }}<br>
        {{ /collection:events }}

        <h2>Ongoing Events</h2>
        {{ collection:events start_date:is_after=now end_date:is_after=now }}
        {{ title }}<br>
        {{ /collection:events }}

        <h2>Past Events</h2>
        {{ collection:events start_date:is_before=now end_date:is_before=now }}
        {{ title }}<br>
        {{ /collection:events }}

Within the collection:events lines the start/end fields are obviously wrong here as I'm not mentioning the range field called date_range. But any way I try to do it results in nothing happening.

I've tried this:

{{ collection:events date_range.start:is_before=now date_range.end:is_after=now }}

And this:

{{ collection:events date_range:start:is_before=now date_range:end:is_after=now }}

Struggling to see what else can be done here.

I've been through the following docs and found nothing covering this:

  • https://statamic.dev/fieldtypes/date#date-ranges
  • https://statamic.dev/tags/collection

Is this even possible or am I going to have to write something custom?

Thanks

Answered by Jack McDade!
>>>>>>> Answered <<<<<<<
5 Replies
5 Followers