<p>There's no way to simply add it to that query you have. But you can use the following workaround.</p>

<p>You first do all the logic within such conditionals you actually wanted to avoid, but instead of getting the ElementModels directly, you do an extra step and get / collect the entries' IDs using the <code>ids()</code> method.</p>

<p>When you're ready, use these IDs with the <code>id</code> property in a second ElementCriteriaModel. This you can paginate like you want!</p>

<p><strong>Edit:</strong><br>Here's some example code!</p>

<pre><code>{% set cutoffDate = 'now'|date_modify('- ' ~ '7' ~ ' days') %}
{% set events = craft.entries.section('events').order('dateStart desc') %}

{% set eventIds = [] %}
{% for event in events if (event.dateEnd ? event.dateEnd : event.dateStart) &gt;= cutoffDate %}
    {% set eventIds = eventIds|merge([event.id]) %}
{% endfor %}

{% set filteredEvents = craft.entries.id(eventIds).fixedOrder('true') %}
</code></pre>

CC BY-SA 3.0
2014-12-01T17:22:26.467Z
Worked perfectly!
hamishtaplin
CC BY-SA 3.0
2014-11-29T01:27:36.073Z
2014-11-29T20:13:22.457Z
2014-11-29T20:13:22.457Z
carlcs
<p>I have an 'whatsOn' structure whose entries include two date fields: 'dateStart' and 'dateEnd'. The end date will be empty if the event is a one-dayer. I want to get all future events using the dateEnd (if not empty) or dateStart if not. So basically, all events that haven't ended yet. How do I add the 'dateEnd' bit to the following query:</p>

<pre><code>{% set allEvents = craft.entries({
    section: 'whatsOn',
    limit: 20,
    level: 2,
    order: 'dateStart',
    dateStart: '&gt;= ' ~ now
}) %}
</code></pre>

<p>Crucially, I'll be paginating them so I need this to return an ElementCriteriaModel, I can't just do the check inside a loop.</p>

<p>EDIT: I just noticed that date fields are just empty (not null/undefined) if you don't select or enter a date. Amended question to reflect this.</p>

CC BY-SA 3.0
2014-12-01T10:36:40.390Z
Updated my answer with some example code!
carlcs
<entries>
Get entries filtered by date fields, one of which might be empty (and paginate them)
# Get entries filtered by date fields, one of which might be empty (and paginate them)

(Asked by: username_0 on 2014-11-28)

I have an 'whatsOn' structure whose entries include two date fields: 'dateStart' and 'dateEnd'. The end date will be empty if the event is a one-dayer. I want to get all future events using the dateEnd (if not empty) or dateStart if not. So basically, all events that haven't ended yet. How do I add the 'dateEnd' bit to the following query:
{% set allEvents = craft.entries({
    section: 'whatsOn',
    limit: 20,
    level: 2,
    order: 'dateStart',
    dateStart: '>= ' ~ now
}) %}

Crucially, I'll be paginating them so I need this to return an ElementCriteriaModel, I can't just do the check inside a loop.
EDIT: I just noticed that date fields are just empty (not null/undefined) if you don't select or enter a date. Amended question to reflect this.

**Tags:** entries
> **Comments:**
> **username_1**: Updated my answer with some example code!  

---

## 1 answers

### Answer by username_1 on 2014-11-29. Score: 2 | Accepted answer

There's no way to simply add it to that query you have. But you can use the following workaround.
You first do all the logic within such conditionals you actually wanted to avoid, but instead of getting the ElementModels directly, you do an extra step and get / collect the entries' IDs using the ids() method.
When you're ready, use these IDs with the id property in a second ElementCriteriaModel. This you can paginate like you want!
Edit:Here's some example code!
{% set cutoffDate = 'now'|date_modify('- ' ~ '7' ~ ' days') %}
{% set events = craft.entries.section('events').order('dateStart desc') %}

{% set eventIds = [] %}
{% for event in events if (event.dateEnd ? event.dateEnd : event.dateStart) >= cutoffDate %}
    {% set eventIds = eventIds|merge([event.id]) %}
{% endfor %}

{% set filteredEvents = craft.entries.id(eventIds).fixedOrder('true') %}

> **Comments:**
> **username_0**: Worked perfectly!  

---

