<p><strong>Update</strong></p>

<p>Found the solution for my first question.</p>

<p>For current events my working solution is:</p>

<pre><code>{% set entries = craft.entries()
  .section('messages')
  .type('event')
  .startDate('&lt;= ' ~ now|date('c'))
  .endDate('&gt;= ' ~ now|date('c'))
  .orderBy('startDate asc')
  .all()
%}

{% for entry in entries %}
  &lt;h1&gt;{{ entry.title }}&lt;/h1&gt;
{% endfor %}
</code></pre>

<p>For upcoming events a simpler solution is:</p>

<pre><code>{% set entries = craft.entries()
  .section('messages')
  .type('event')
  .startDate('&gt; ' ~ now|date('c'))
  .orderBy('startDate asc')
  .all()
%}

{% for entry in entries %}
    &lt;h1&gt;{{ entry.title }}&lt;/h1&gt;
{% endfor %}
</code></pre>

<p>I can't figure out how to get my second question working. </p>

<p>I need to query all messages (type: news and events) and exclude current and upcoming events. The output: all messages of type news and all messages of type events that happened in the past. Including pagination.</p>

<p>It should be a combination of this i think:</p>

<pre><code>{% set news = craft.entries()
  .section('messages')
  .type('news')
  .all()
%}

{% set events = craft.entries()
  .section('messages')
  .type('event')
  .startDate('&lt; ' ~ now|date('c'))
  .endDate('&lt; ' ~ now|date('c'))
  .all()
%}
</code></pre>

<p>Together combined with ordering by post date and pagination. Help! :-)</p>

CC BY-SA 3.0
2018-04-26T21:22:11.740Z
2018-04-26T21:29:59.513Z
2018-04-26T21:29:59.513Z
Rinus
<p>I'm having the following layout on a page: a messages page that shows events and news that are grouped in a section called messages. The page starts with current events, then upcoming events and finishes with all messages (old events and news). </p>

<p>The queries i need to run:</p>

<p><strong>Current events</strong></p>

<ul>
<li>section <strong>messages</strong></li>
<li>entry type <strong>event</strong></li>
<li>where <strong>startdate is = or &lt; now</strong> (starting today or earlier)</li>
<li>and <strong>enddate is = or > now</strong> (ending today or later)</li>
<li>ordered by <strong>startdate</strong></li>
</ul>

<p><strong>Upcoming events</strong></p>

<ul>
<li>section <strong>messages</strong></li>
<li>entry type <strong>event</strong></li>
<li>where <strong>startdate is > now</strong> (starting later than today)</li>
<li>ordered by <strong>startdate</strong></li>
</ul>

<p><strong>All messages</strong></p>

<ul>
<li>section <strong>messages</strong></li>
<li>entry type <strong>all</strong> (event and news)</li>
<li>that exclude <strong>current events</strong></li>
<li>and exclude <strong>upcoming events</strong></li>
<li>with <strong>pagination</strong></li>
</ul>

<p>My current code where some parts are working:</p>

<p><strong>Current events</strong></p>

<pre><code>{% set entries = craft.entries.section("messages").type("event").orderBy('startDate asc').all() %}
{% for entry in entries %}
  {% if entry.startDate &lt; now and entry.endDate &gt; now  %}
    &lt;h1&gt;{{ entry.title }}&lt;/h1&gt;
  {% endif %}
{% endfor %}
</code></pre>

<p><strong>Upcoming events</strong></p>

<pre><code>{% set entries = craft.entries.section('messages').type("event").orderBy('startDate asc').all() %}
{% for entry in entries %}
  {% if entry.startDate &gt; now %}
    &lt;h1&gt;{{ entry.title }}&lt;/h1&gt;
  {% endif %}          
{% endfor %}
&lt;/section&gt;
</code></pre>

<p><strong>All messages</strong></p>

<pre><code>{% paginate craft.entries.section("messages").limit(9) as pageInfo, pageEntries %}
{% for entry in pageEntries %}
  &lt;h1&gt;{{ entry.title }}&lt;/h1&gt;
{% endfor %}
&lt;ul&gt;
  {% for page, url in pageInfo.getPrevUrls(5) %}
    &lt;li&gt;&lt;a href="{{ url }}"&gt;{{ page }}&lt;/a&gt;&lt;/li&gt;
  {% endfor %}
    &lt;li&gt;{{ pageInfo.currentPage }}&lt;/li&gt;
  {% for page, url in pageInfo.getNextUrls(5) %}
    &lt;li&gt;&lt;a&gt;{{ page }}&lt;/a&gt;&lt;/li&gt;
  {% endfor %}
&lt;/ul&gt;
</code></pre>

<p><strong>Two questions i have:</strong></p>

<p>How do i query current events so startDate is &lt; AND =? And endDate is > AND =?</p>

<p>And how do i query all messages (news and events), excluding current and upcoming events but with past events?</p>

CC BY-SA 3.0
2018-04-25T14:47:58.583Z
Possible duplicate of [How to get entries between a time](https://craftcms.stackexchange.com/questions/25772/how-to-get-entries-between-a-time) Your question is actually a combination of different questions that are all answered multiple times within the last few days
Robin Schambach
CC BY-SA 3.0
2018-04-26T21:22:43.477Z
Thanks. Found the solution to my first question, see post below. Still looking for an answer to question two. :-)
Rinus
<query>
Query section entries based on start/end date and type
# Query section entries based on start/end date and type

(Asked by: username_0 on 2018-04-25)

I'm having the following layout on a page: a messages page that shows events and news that are grouped in a section called messages. The page starts with current events, then upcoming events and finishes with all messages (old events and news). 
The queries i need to run:
Current events

section messages
entry type event
where startdate is = or < now (starting today or earlier)
and enddate is = or > now (ending today or later)
ordered by startdate

Upcoming events

section messages
entry type event
where startdate is > now (starting later than today)
ordered by startdate

All messages

section messages
entry type all (event and news)
that exclude current events
and exclude upcoming events
with pagination

My current code where some parts are working:
Current events
{% set entries = craft.entries.section("messages").type("event").orderBy('startDate asc').all() %}
{% for entry in entries %}
  {% if entry.startDate < now and entry.endDate > now  %}
    {{ entry.title }}
  {% endif %}
{% endfor %}

Upcoming events
{% set entries = craft.entries.section('messages').type("event").orderBy('startDate asc').all() %}
{% for entry in entries %}
  {% if entry.startDate > now %}
    {{ entry.title }}
  {% endif %}          
{% endfor %}


All messages
{% paginate craft.entries.section("messages").limit(9) as pageInfo, pageEntries %}
{% for entry in pageEntries %}
  {{ entry.title }}
{% endfor %}

  {% for page, url in pageInfo.getPrevUrls(5) %}
    {{ page }}
  {% endfor %}
    {{ pageInfo.currentPage }}
  {% for page, url in pageInfo.getNextUrls(5) %}
    {{ page }}
  {% endfor %}


Two questions i have:
How do i query current events so startDate is < AND =? And endDate is > AND =?
And how do i query all messages (news and events), excluding current and upcoming events but with past events?

**Tags:** query
> **Comments:**
> **username_1**: Possible duplicate of [How to get entries between a time](https://craftcms.stackexchange.com/questions/25772/how-to-get-entries-between-a-time) Your question is actually a combination of different questions that are all answered multiple times within the last few days  
> **username_0**: Thanks. Found the solution to my first question, see post below. Still looking for an answer to question two. :-)  

---

## 1 answers

### Answer by username_0 on 2018-04-26. Score: 2

Update
Found the solution for my first question.
For current events my working solution is:
{% set entries = craft.entries()
  .section('messages')
  .type('event')
  .startDate('<= ' ~ now|date('c'))
  .endDate('>= ' ~ now|date('c'))
  .orderBy('startDate asc')
  .all()
%}

{% for entry in entries %}
  {{ entry.title }}
{% endfor %}

For upcoming events a simpler solution is:
{% set entries = craft.entries()
  .section('messages')
  .type('event')
  .startDate('> ' ~ now|date('c'))
  .orderBy('startDate asc')
  .all()
%}

{% for entry in entries %}
    {{ entry.title }}
{% endfor %}

I can't figure out how to get my second question working. 
I need to query all messages (type: news and events) and exclude current and upcoming events. The output: all messages of type news and all messages of type events that happened in the past. Including pagination.
It should be a combination of this i think:
{% set news = craft.entries()
  .section('messages')
  .type('news')
  .all()
%}

{% set events = craft.entries()
  .section('messages')
  .type('event')
  .startDate('< ' ~ now|date('c'))
  .endDate('< ' ~ now|date('c'))
  .all()
%}

Together combined with ordering by post date and pagination. Help! :-)


---

