<p>No need to loop. You can use <code>random()</code>:</p>

<pre><code>{% set randomEntry = random(entry.entriesFieldHandle) %}
{% if randomEntry %}
    {% set randomUrl = randomEntry.url %}
{% endif %}
</code></pre>

<p><code>randomUrl</code> is your random entry's url you wanted to get.</p>

CC BY-SA 3.0
2014-07-12T14:40:53.117Z
[random](http://twig.sensiolabs.org/doc/functions/random.html) is a twig function. `random(entry.entriesFieldHandle)` will retrieve all the entries, then pick a random one.
Marion Newlevant
CC BY-SA 3.0
2014-07-12T14:43:14.007Z
@Marion Is anything bad in it? OP seems to ask about twig...  Sorry, not really so experienced :-)
nicael
CC BY-SA 3.0
2014-07-12T14:47:52.430Z
Nothing wrong with it. Just responding to @ChristianSeelbach comment on my answer. Picking the random entry with the query is probably more efficient if there are a lot of entries, but your code is simpler. Should check that it found something though.
Marion Newlevant
CC BY-SA 3.0
2014-07-12T14:50:02.797Z
@Marion You mean if it found an entry? Well, why not...
nicael
CC BY-SA 3.0
2014-07-12T14:51:00.153Z
@micael yes. If there are no entries, randomEntry.url will give an error
Marion Newlevant
CC BY-SA 3.0
2014-07-12T14:51:40.920Z
@Marion Now? :-) And I seem to be nicael, not micael :D And no need to add `@reply` if you respond to the post author - he is automatically notified.
nicael
CC BY-SA 3.0
2014-07-11T23:27:25.310Z
2014-07-12T14:50:54.587Z
2014-07-12T14:50:54.587Z
nicael
<p>It is possible to have the query pick a random entry.
From the Craft <a href="http://buildwithcraft.com/docs/entries-fields#templating" rel="nofollow">Entries Fields</a> docs we see that <code>entry.entriesFieldHandle</code> is a shortcut for</p>

<pre><code>craft.entries({
  relatedTo: { sourceElement: entry, field: "entriesFieldHandle" },
  order: "sortOrder",
  limit: null
})
</code></pre>

<p>And what we want is to order the entries randomly, and then limit the number we retrieve to only one:</p>

<pre><code>craft.entries({
  relatedTo: { sourceElement: entry, field: "entriesFieldHandle" },
  order: "RAND()",
  limit: 1
})
</code></pre>

<p>So to retrieve one random entry and get its url:</p>

<pre><code>{% set randomEntry = craft.entries({
      relatedTo: { sourceElement: entry, field: "entriesFieldHandle" },
      order: "RAND()",
      limit: 1
    }).first()
%}
{% if randomEntry %}
  {% set randomUrl = randomEntry.url %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2014-07-12T11:37:38.187Z
Interesting answer, Marion! Performance is better with your second approach, right? Then I'd leave out the random() part or put it at the end of the answer.
carlcs
CC BY-SA 3.0
2014-07-13T06:58:50.943Z
_(Sorry for the repost, I found a big error in my old comment.)_ Very cool way to do it! Using your last example I keep getting randomEntry returning as null. All I've changed is my "field:" to "aGlobalSet.entriesField". Any ideas why it isn't working?
Simon Kuran
CC BY-SA 3.0
2014-07-13T23:21:58.047Z
@SimonKuran to pick a random entry from `aGlobalSet` you want `{sourceElement: aGlobalSet, field: "entriesField"}`
Marion Newlevant
CC BY-SA 3.0
2014-07-13T23:45:24.950Z
Ah, I get it now. It worked, thanks! I put an edit in the queue adding the "all at once" technique I used to your answer.
Simon Kuran
CC BY-SA 3.0
2014-07-14T00:04:49.717Z
I think the more common use case is that someone wants a random entry (with title, url, etc.), not just the url of a random entry
Marion Newlevant
CC BY-SA 3.0
2014-07-14T00:38:57.060Z
Oh, I'm sure it is. My example just better matched the question, but it's fairly self-explanatory to simplify down to it. It's totally fine added or not. :)
Simon Kuran
CC BY-SA 3.0
2014-07-12T04:18:15.633Z
2014-07-12T14:40:30.257Z
2014-07-12T14:40:30.257Z
Marion Newlevant
<p>I have a Entries field and I'd like to return a URL for one randomly picked entry.</p>

<p>I could loop through all the entries, but I'd like to do something more efficient. Can I return an array of URLs? Or better yet, is it possible to randomly get one URL all at once?</p>

<templating><entries><url><random>
How do I get a random entry's URL from a Entries field?
# How do I get a random entry's URL from a Entries field?

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

I have a Entries field and I'd like to return a URL for one randomly picked entry.

I could loop through all the entries, but I'd like to do something more efficient. Can I return an array of URLs? Or better yet, is it possible to randomly get one URL all at once?

**Tags:** templating, entries, url, random


---

## 2 answers

### Answer by username_2 on 2014-07-12. Score: 7 | Accepted answer

It is possible to have the query pick a random entry.
From the Craft Entries Fields docs we see that entry.entriesFieldHandle is a shortcut for
craft.entries({
  relatedTo: { sourceElement: entry, field: "entriesFieldHandle" },
  order: "sortOrder",
  limit: null
})

And what we want is to order the entries randomly, and then limit the number we retrieve to only one:
craft.entries({
  relatedTo: { sourceElement: entry, field: "entriesFieldHandle" },
  order: "RAND()",
  limit: 1
})

So to retrieve one random entry and get its url:
{% set randomEntry = craft.entries({
      relatedTo: { sourceElement: entry, field: "entriesFieldHandle" },
      order: "RAND()",
      limit: 1
    }).first()
%}
{% if randomEntry %}
  {% set randomUrl = randomEntry.url %}
{% endif %}

> **Comments:**
> **username_3**: Interesting answer, Marion! Performance is better with your second approach, right? Then I'd leave out the random() part or put it at the end of the answer.  
> **username_0**: _(Sorry for the repost, I found a big error in my old comment.)_ Very cool way to do it! Using your last example I keep getting randomEntry returning as null. All I've changed is my "field:" to "aGlobalSet.entriesField". Any ideas why it isn't working?  
> **username_2**: @SimonKuran to pick a random entry from `aGlobalSet` you want `{sourceElement: aGlobalSet, field: "entriesField"}`  
> **username_0**: Ah, I get it now. It worked, thanks! I put an edit in the queue adding the "all at once" technique I used to your answer.  
> **username_2**: I think the more common use case is that someone wants a random entry (with title, url, etc.), not just the url of a random entry  
> **username_0**: Oh, I'm sure it is. My example just better matched the question, but it's fairly self-explanatory to simplify down to it. It's totally fine added or not. :)  

---

### Answer by username_1 on 2014-07-11. Score: 3

No need to loop. You can use random():
{% set randomEntry = random(entry.entriesFieldHandle) %}
{% if randomEntry %}
    {% set randomUrl = randomEntry.url %}
{% endif %}

randomUrl is your random entry's url you wanted to get.

> **Comments:**
> **username_2**: [random](http://twig.sensiolabs.org/doc/functions/random.html) is a twig function. `random(entry.entriesFieldHandle)` will retrieve all the entries, then pick a random one.  
> **username_1**: @Marion Is anything bad in it? OP seems to ask about twig...  Sorry, not really so experienced :-)  
> **username_2**: Nothing wrong with it. Just responding to @ChristianSeelbach comment on my answer. Picking the random entry with the query is probably more efficient if there are a lot of entries, but your code is simpler. Should check that it found something though.  
> **username_1**: @Marion You mean if it found an entry? Well, why not...  
> **username_2**: @micael yes. If there are no entries, randomEntry.url will give an error  
> **username_1**: @Marion Now? :-) And I seem to be username_1, not micael :D And no need to add `@reply` if you respond to the post author - he is automatically notified.  

---

