TransWikia.com

Query for multiple date ranges on a date field

Craft CMS Asked on January 17, 2021

I’m able to successfully query a date field for a date range using this syntax:

{% set entries = entries.startsOn(['and', '>= '~firstDay, '< '~ lastDay]) %}

What I would like to do however is query that field for multiple date ranges at once with an or operator.

I can do this with other fields but the date range already using an and operator complicates matters so I can’t do the same.

{% set data = data|merge(['or']) %}
{% for item in items %}
     {% set data = data|merge(['*'~item~'*']) %}
{% endfor %}

Any suggestions gratefully received!

One Answer

You can use two queries, each for each range, and then merge them like this:

{% set firstRange = craft.entries({fieldHandle: ['and', '>= '~date1FirstDay, '< '~ date1LastDay]}) %}
{% set secondRange = craft.entries({fieldHandle: ['and', '>= '~date2FirstDay, '< '~ date2LastDay]}) %}

{% set entries = firstRange |merge(secondRange) %}

This is the way to do it. Also you should query for the ids, and then merge those, and then query by id.

I hope this will help.

Answered by ZealousWeb on January 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP