<p>I had this exact same problem a while ago (calender view with a start date and an optional end date). I looked into how to combine multiple field filters with an OR-operator – as far as I can tell, it's not possible using criteria arrays or field-methods on the element query.</p>
<p>That leaves you with a <a href="https://craftcms.com/docs/3.x/element-queries.html#conditions" rel="nofollow noreferrer">custom <code>where()</code> condition</a>. Formalized, the conditions for a current or future events are as follows:</p>
<ol>
<li>The <code>eventStartDate</code> must not be empty. If the field is required in your setup, you don't have to check that in the query.</li>
<li>Either the <code>eventStartDate</code> OR the <code>eventEndDate</code> must be in the future.</li>
</ol>
<p>Adding the other parameters to your query, you can use something like this:</p>
<pre class="lang-twig prettyprint-override"><code>{% set relatedEvents = craft.entries()
    .section('schoolsAndEducators')
    .id('not ' ~ entry.id)
    .eventStartDate(':notempty:')
    .andWhere([
        'or',
        ['&gt;=', '[[field_eventStartDate_xxxxxxxx]]', now|atom],
        ['&gt;=', '[[field_eventEndDate_xxxxxxxx]]', now|atom],
    ])
    .orderBy('RAND()')
    .limit(3)
    .all()
%}
</code></pre>
<p>A couple of notes regarding the field column names:</p>
<ul>
<li>The <code>field_</code> prefix is used for all custom fields.</li>
<li>All field columns get a suffix based on the fields UID since Craft 3.7. You need to replace the <code>xxxxxxxx</code> with the field suffix for your fields. You can use the helper method <code>ElementHelper::fieldColumnFromField</code>, see the discussions <a href="https://craftcms.stackexchange.com/questions/38471/get-array-of-titles-instead-of-whole-collection-of-entries-attributes">here</a> and <a href="https://github.com/craftcms/cms/issues/6922" rel="nofollow noreferrer">here</a> for details. I've posted two options for using the field suffix in Twig in <a href="https://craftcms.stackexchange.com/a/38473/11705">my answer here</a>. Alternatively, you can just hardcode the field suffix in your template – it's based on the UID, so it won't change between environments.</li>
</ul>

CC BY-SA 4.0
2021-11-19T15:08:54.183Z
Well that's just marvellous, worked like an absolute charm. I hardcoded the suffix because while it does feel weird to hardcode it, if it doesn't change then why give myself the extra bother lol. Thanks for coming to the rescue yet again! Can you tell I don't know what I'm doing lol
Grace
CC BY-SA 4.0
2021-11-19T15:17:03.800Z
Yeah I ended up hardcoding the suffix in my template as well … ^^ Nobody has it all figured out, the important thing is to ask the right questions and stay curious!
MoritzLost
CC BY-SA 4.0
2021-11-19T13:18:27.343Z
2021-11-19T13:18:27.343Z
MoritzLost
<p>I have an entry which shows related events, based on the entry category, at the bottom. These events all have a start date and an end date - the end date is required, if the event is only one day, eventEndDate should be set to the same date as eventStartDate.</p>
<p>I have created a query with parameters to pull in related events, but only want events that are in the future. I need the query to take into account both start and end date - for example, the start date may have passed, but if the end date is still in the future this event can be shown. If both have passed, it should not show.</p>
<p>My difficulty is having the query treat these two parameters as 'OR' rather than 'AND'. I can't find docs on the correct syntax to achieve this. I have shown below the query as I currently have it. It establishes the current entry category; sets the parameters for section, checking for the start date being in the future and limits to 3 events, and then uses these parameters to pull in any related events that are not the current one.</p>
<pre><code>{% set category = entry.eventCategory %}
{% set relatedParms = { section : 'schoolsAndEducators', eventStartDate: &quot;&gt; &quot; ~ (now|atom), limit: '3' } %}
{% set relatedEvents = craft.entries(relatedParms).id('not ' ~ entry.id).orderBy('RAND()').all() %}
</code></pre>
<p>I'm not sure if it is possible, but I need to modify that 'relatedParams' query to also take eventEndDate into account. The below does not work, as it treats it like an 'AND' statement and only pulls in events for which both dates have passed:</p>
<pre><code>{% set relatedParms = { section : 'schoolsAndEducators', eventStartDate: &quot;&gt; &quot; ~ (now|atom), eventEndDate: &quot;&gt; &quot; ~ (now|atom), limit: '3' } %}
</code></pre>
<p>I have checked similar questions here but either they aren't the same situation or I can't figure how to adjust the code for my particular requirements.</p>

CC BY-SA 4.0
2021-11-21T23:43:15.027Z
Any reason why you can't just drop the ``startDate`` in the query? Unless I've read your question incorrectly,  If the start + end dates are the same in some instances, you're really going to be querying just the ``endDate`` anyway.  Also since you're really only looking for dates that aren't "expired" yet, the start date doesn't really matter. The only time you'd want to query the start date would be if you wanted to look for events in a small sliver of time (eg "beginning next week but before next month) but that doesn't seem to be the case here?
RitterKnight
CC BY-SA 4.0
2021-11-22T15:23:30.813Z
@RitterKnight ... You make a most interesting point my friend lol. I think you're completely right. To be perfectly honest, I think I didn't realise this because it started out that only the start date was required and then changed because it was causing more issues. So I had to check for both because they may not always have entered an end date. But now that they do, yes you're correct and I really only need to check the end date. After all this!! Clearly just getting my brain tied up in knots over this project!
Grace
CC BY-SA 4.0
2021-11-22T23:44:30.670Z
I've been there! Sometimes the best solution is the simplest. (But sometimes not...)
RitterKnight
<query>
Using 'or' within query parameters
# Using 'or' within query parameters

(Asked by: username_0 on 2021-11-19)

I have an entry which shows related events, based on the entry category, at the bottom. These events all have a start date and an end date - the end date is required, if the event is only one day, eventEndDate should be set to the same date as eventStartDate.
I have created a query with parameters to pull in related events, but only want events that are in the future. I need the query to take into account both start and end date - for example, the start date may have passed, but if the end date is still in the future this event can be shown. If both have passed, it should not show.
My difficulty is having the query treat these two parameters as 'OR' rather than 'AND'. I can't find docs on the correct syntax to achieve this. I have shown below the query as I currently have it. It establishes the current entry category; sets the parameters for section, checking for the start date being in the future and limits to 3 events, and then uses these parameters to pull in any related events that are not the current one.
{% set category = entry.eventCategory %}
{% set relatedParms = { section : 'schoolsAndEducators', eventStartDate: "> " ~ (now|atom), limit: '3' } %}
{% set relatedEvents = craft.entries(relatedParms).id('not ' ~ entry.id).orderBy('RAND()').all() %}

I'm not sure if it is possible, but I need to modify that 'relatedParams' query to also take eventEndDate into account. The below does not work, as it treats it like an 'AND' statement and only pulls in events for which both dates have passed:
{% set relatedParms = { section : 'schoolsAndEducators', eventStartDate: "> " ~ (now|atom), eventEndDate: "> " ~ (now|atom), limit: '3' } %}

I have checked similar questions here but either they aren't the same situation or I can't figure how to adjust the code for my particular requirements.

**Tags:** query
> **Comments:**
> **username_1**: Any reason why you can't just drop the ``startDate`` in the query? Unless I've read your question incorrectly,  If the start + end dates are the same in some instances, you're really going to be querying just the ``endDate`` anyway.  Also since you're really only looking for dates that aren't "expired" yet, the start date doesn't really matter. The only time you'd want to query the start date would be if you wanted to look for events in a small sliver of time (eg "beginning next week but before next month) but that doesn't seem to be the case here?  
> **username_0**: @RitterKnight ... You make a most interesting point my friend lol. I think you're completely right. To be perfectly honest, I think I didn't realise this because it started out that only the start date was required and then changed because it was causing more issues. So I had to check for both because they may not always have entered an end date. But now that they do, yes you're correct and I really only need to check the end date. After all this!! Clearly just getting my brain tied up in knots over this project!  
> **username_1**: I've been there! Sometimes the best solution is the simplest. (But sometimes not...)  

---

## 1 answers

### Answer by username_2 on 2021-11-19. Score: 2 | Accepted answer

I had this exact same problem a while ago (calender view with a start date and an optional end date). I looked into how to combine multiple field filters with an OR-operator – as far as I can tell, it's not possible using criteria arrays or field-methods on the element query.
That leaves you with a custom where() condition. Formalized, the conditions for a current or future events are as follows:

The eventStartDate must not be empty. If the field is required in your setup, you don't have to check that in the query.
Either the eventStartDate OR the eventEndDate must be in the future.

Adding the other parameters to your query, you can use something like this:
{% set relatedEvents = craft.entries()
    .section('schoolsAndEducators')
    .id('not ' ~ entry.id)
    .eventStartDate(':notempty:')
    .andWhere([
        'or',
        ['>=', '[[field_eventStartDate_xxxxxxxx]]', now|atom],
        ['>=', '[[field_eventEndDate_xxxxxxxx]]', now|atom],
    ])
    .orderBy('RAND()')
    .limit(3)
    .all()
%}

A couple of notes regarding the field column names:

The field_ prefix is used for all custom fields.
All field columns get a suffix based on the fields UID since Craft 3.7. You need to replace the xxxxxxxx with the field suffix for your fields. You can use the helper method ElementHelper::fieldColumnFromField, see the discussions here and here for details. I've posted two options for using the field suffix in Twig in my answer here. Alternatively, you can just hardcode the field suffix in your template – it's based on the UID, so it won't change between environments.

> **Comments:**
> **username_0**: Well that's just marvellous, worked like an absolute charm. I hardcoded the suffix because while it does feel weird to hardcode it, if it doesn't change then why give myself the extra bother lol. Thanks for coming to the rescue yet again! Can you tell I don't know what I'm doing lol  
> **username_2**: Yeah I ended up hardcoding the suffix in my template as well … ^^ Nobody has it all figured out, the important thing is to ask the right questions and stay curious!  

---

