<p>Found the solution.</p>

<p>First get entries from section events as ID's and check endDate &lt; now:</p>

<pre><code>{% set eventsIds = craft.entries()
  .section('events')
  .endDate('&lt; ' ~ now|date('c'))
  .ids()
%}
</code></pre>

<p>Then get entries from section news as ID's:</p>

<pre><code>{% set newsIds = craft.entries()
  .section('news')
  .ids()
%}
</code></pre>

<p>Then merge those two together:</p>

<pre><code>{% set entryIds = eventsIds|merge(newsIds) %}
</code></pre>

<p>Then paginate by id, set orderBy postDate and limit to 9:</p>

<pre><code>{% paginate craft.entries()
  .id(entryIds)
  .orderBy('postDate desc')
  .limit(9)
  as pageInfo, pageEntries
%}
</code></pre>

CC BY-SA 4.0
2018-05-02T10:49:11.633Z
2018-05-02T10:49:11.633Z
Rinus
<p>I'm having a page that shows entries from two sections: news and events.</p>

<p>The page has the following layout:</p>

<hr>

<p><strong>All current event entries</strong></p>

<ul>
<li>Current event entry 1</li>
<li>Current event entry 2</li>
</ul>

<hr>

<p><strong>All upcoming event entries</strong></p>

<ul>
<li>Upcoming event entry 1</li>
<li>Upcoming event entry 2</li>
</ul>

<hr>

<p><strong>All news entries and all past event entries combined including pagination</strong></p>

<ul>
<li>News entry 1</li>
<li>Past event entry 2</li>
<li>Past event entry 3</li>
<li>News entry 4</li>
<li>News entry 5</li>
<li>Past event entry 6</li>
</ul>

<hr>

<p>To query the current events i use the following query</p>

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

<p>To query the upcoming events i use the following query</p>

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

<p>I have no idea how to query all the news and past event entries. </p>

<p>The query should include:</p>

<ul>
<li>Query all entries from section news</li>
<li>Query all events from section events where entry.endDate &lt; now</li>
<li>Sort all entries by post date</li>
<li>Limit to 9 and include pagination</li>
</ul>

<p>I came this far with the query:</p>

<pre><code>{% paginate craft.entries()
  .section(['events','news'])
  .limit(9)
  .orderBy('postDate asc')
  as pageInfo, pageEntries
%}
</code></pre>

<p>What is missing: checking the event entries to see if the endDate &lt; now. </p>

<p>I can't just add .endDate('&lt; ' ~ now|date('c')) to the query. That would hide all the news entries, ofcourse. </p>

<p>I did search and read a lot of posts on stackexchange. Couldn't find a solution.</p>

<p>Anyone that could help me with this?</p>

CC BY-SA 4.0
2018-05-02T08:14:26.400Z
Would it be an option to use PHP for it?
Robin Schambach
CC BY-SA 4.0
2018-05-02T09:16:56.277Z
I would rather stick to Twig. But, how would you solve this with PHP?
Rinus
CC BY-SA 4.0
2018-05-02T09:37:35.317Z
I would fetch the element ids with two separate queries and populate all elements in the end
Robin Schambach
CC BY-SA 4.0
2018-05-02T10:42:47.497Z
Ah yes, ID's is the trick. Will post the solution below.
Rinus
<query>
Query entries from two sections, check date field for one section and include pagination
# Query entries from two sections, check date field for one section and include pagination

(Asked by: username_0 on 2018-05-02)

I'm having a page that shows entries from two sections: news and events.
The page has the following layout:

All current event entries

Current event entry 1
Current event entry 2


All upcoming event entries

Upcoming event entry 1
Upcoming event entry 2


All news entries and all past event entries combined including pagination

News entry 1
Past event entry 2
Past event entry 3
News entry 4
News entry 5
Past event entry 6


To query the current events i use the following query
{% set entries = craft.entries()
  .section('events')
  .startDate('<= ' ~ now|date('c'))
  .endDate('>= ' ~ now|date('c'))
  .orderBy('startDate asc')
  .all()
%}

To query the upcoming events i use the following query
{% set entries = craft.entries()
  .section('events')
  .startDate('> ' ~ now|date('c'))
  .orderBy('startDate asc')
  .all()
%}

I have no idea how to query all the news and past event entries. 
The query should include:

Query all entries from section news
Query all events from section events where entry.endDate < now
Sort all entries by post date
Limit to 9 and include pagination

I came this far with the query:
{% paginate craft.entries()
  .section(['events','news'])
  .limit(9)
  .orderBy('postDate asc')
  as pageInfo, pageEntries
%}

What is missing: checking the event entries to see if the endDate < now. 
I can't just add .endDate('< ' ~ now|date('c')) to the query. That would hide all the news entries, ofcourse. 
I did search and read a lot of posts on stackexchange. Couldn't find a solution.
Anyone that could help me with this?

**Tags:** query

> **Comments:**
> **username_1**: Would it be an option to use PHP for it?  
> **username_0**: I would rather stick to Twig. But, how would you solve this with PHP?  
> **username_1**: I would fetch the element ids with two separate queries and populate all elements in the end  
> **username_0**: Ah yes, ID's is the trick. Will post the solution below.  

---

## 1 answers

### Answer by username_0 on 2018-05-02. Score: 1

Found the solution.
First get entries from section events as ID's and check endDate < now:
{% set eventsIds = craft.entries()
  .section('events')
  .endDate('< ' ~ now|date('c'))
  .ids()
%}

Then get entries from section news as ID's:
{% set newsIds = craft.entries()
  .section('news')
  .ids()
%}

Then merge those two together:
{% set entryIds = eventsIds|merge(newsIds) %}

Then paginate by id, set orderBy postDate and limit to 9:
{% paginate craft.entries()
  .id(entryIds)
  .orderBy('postDate desc')
  .limit(9)
  as pageInfo, pageEntries
%}


---

