studioespresso / craft-date-range

Date range field for Craft CMS
MIT License
12 stars 5 forks source link

How to query ongoing and future dates? #31

Closed garycrane closed 2 years ago

garycrane commented 2 years ago

Just started working with Date Range field and I'm struggling to query events that are either on-going or in the future. Perhaps another way to put it would be not isPast. At present, I'm getting all isOnGoing IDs, then all isFuture IDs and merging them but it feels a bit of a clunky way to handle it. I'm wondering how I might query all entries that are not isPast?

I'm sure I'm missing something obvious and hoping this is a really straightforward thing to achieve? Any advice greatly appreciated!

daniellelecomte commented 2 years ago

Looking for this as well! 💯

daniellelecomte commented 2 years ago

@garycrane - when you merge your two queries, are you able to order them by start date?

garycrane commented 2 years ago

@daniellelecomte This is working for us. As mentioned above, it's a bit clunky but it's working:

{% set onGoingIds = craft.entries.section('events').isOnGoing('eventDate').ids() %}
{% set futureIds = craft.entries.section('events').isFuture('eventDate').ids() %}
{% set mergeIds = futureIds|merge(onGoingIds) %}

{# Get all events from the merge #}
{% set getEvents = craft.entries.section('events').id(mergeIds) %}
{% set entries = getEvents.orderBy('eventDate asc').all() %}

Hope this helps.

daniellelecomte commented 2 years ago

Thanks so much @garycrane - that's exactly what I did, too. Appreciate you sharing!

janhenckens commented 2 years ago

Hey @daniellelecomte @garycrane

Sorry of the late reply, I was out of the office last week. Good to see you found a solution!

Agreed that it's a bit clunky, I'll have a look at adding something like isOnGoingOrFuture where either the start date or the and end date should be in the future.

garycrane commented 2 years ago

Thanks @janhenckens that would be great. Even isNotPast would work. Simply where the end date is in the future. This would cover both scenarios? Would be great to see something like this implemented, though technically, we do have a solution now. One instance where a native solution might be beneficial is search results listings. Could still do it that with the above I guess, but it gets even clunkier.

One other way to do it (don't know if this is doable) but maybe an option to set entries where the end date has past to 'Disabled', then could run a standard query?

janhenckens commented 2 years ago

Cheers @garycrane, isNotPast was the better option. Release as 2.5.0, feel free to give it a spin :)