

<p><a href="https://docs.craftcms.com/v2/templating/craft.entries.html#parameters" rel="nofollow noreferrer">According to the docs</a>, you can <strong>pass an array</strong> into the "section" parameter:</p>

<pre class="lang-none prettyprint-override"><code>{% set entries = craft.entries.section(['quotes','didYouKnow']) %}
</code></pre>

<p>There are several ways to get a random entry, but the method you've chosen is a very good one, because it randomizes during the initial SQL call:</p>

<pre class="lang-none prettyprint-override"><code>.order('RAND()')
</code></pre>

<p>Lastly, you can cut out some of the extra legwork from your original example by calling <code>first</code> instead of <code>find</code>:</p>

<pre class="lang-none prettyprint-override"><code>{% set entry = craft.entries.section('mySection').first() %}
</code></pre>

<p>When you retrieve your entry using <code>first</code>, you'll get a single entry instead of an array of entries. You can skip the <code>for</code> loop entirely, since you're already dealing with a single entry.</p>

<p><strong>So to put that all together...</strong></p>

<pre class="lang-none prettyprint-override"><code>{% set randomEntry = craft.entries.section(['quotes','didYouKnow']).order('RAND()').first() %}

&lt;h1&gt;{{ randomEntry.title }}&lt;/h1&gt;
</code></pre>

CC BY-SA 3.0
2015-03-07T12:53:07.117Z
Would in this case other filters e.g `postDate desc` work?
Dominik Krulak
CC BY-SA 3.0
2015-03-08T19:06:41.917Z
Other filters work a little differently... here is a good example of [using multiple search filters on the `postDate` field...](http://craftcms.stackexchange.com/a/2875/45)
Lindsey D
CC BY-SA 4.0
2014-08-30T06:38:23.340Z
2020-05-27T06:03:17.817Z
2020-05-27T06:03:17.817Z
Lindsey D
<p>I'm trying to pull a random entry from among an array of sections, but I can't get it to work.</p>

<p>I tried the code below, but I'm just getting errors. I've tried multiple ways of listing the sections -- "section","section" ['section,section'],etc, but none work. Do I need to specify my sections in a different way?</p>

<pre><code>{% set random = craft.entries.section('quotes,didYouKnow').limit(1).order('RAND()').find() %}
{% for content in random %}
...
{% endfor %}
</code></pre>

<templating><entries><sections>
How to get one random entry from among multiple sections
# How to get one random entry from among multiple sections

(Asked by: username_0 on 2014-08-29)

I'm trying to pull a random entry from among an array of sections, but I can't get it to work.
I tried the code below, but I'm just getting errors. I've tried multiple ways of listing the sections -- "section","section" ['section,section'],etc, but none work. Do I need to specify my sections in a different way?
{% set random = craft.entries.section('quotes,didYouKnow').limit(1).order('RAND()').find() %}
{% for content in random %}
...
{% endfor %}

**Tags:** templating, entries, sections

---

## 1 answers

### Answer by username_1 on 2014-08-30. Score: 22 | Accepted answer

According to the docs, you can pass an array into the "section" parameter:
{% set entries = craft.entries.section(['quotes','didYouKnow']) %}

There are several ways to get a random entry, but the method you've chosen is a very good one, because it randomizes during the initial SQL call:
.order('RAND()')

Lastly, you can cut out some of the extra legwork from your original example by calling first instead of find:
{% set entry = craft.entries.section('mySection').first() %}

When you retrieve your entry using first, you'll get a single entry instead of an array of entries. You can skip the for loop entirely, since you're already dealing with a single entry.
So to put that all together...
{% set randomEntry = craft.entries.section(['quotes','didYouKnow']).order('RAND()').first() %}

{{ randomEntry.title }}

> **Comments:**
> **username_2**: Would in this case other filters e.g `postDate desc` work?  
> **username_1**: Other filters work a little differently... here is a good example of [using multiple search filters on the `postDate` field...](http://craftcms.stackexchange.com/a/2875/45)  

---

