Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

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 %}
share|improve this answer
    
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 Jul 12 at 11:37
    
(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 Jul 13 at 6:58
    
@SimonKuran to pick a random entry from aGlobalSet you want {sourceElement: aGlobalSet, field: "entriesField"} –  Marion Newlevant Jul 13 at 23:21
    
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 Jul 13 at 23:45
    
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 Jul 14 at 0:04

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.

share|improve this answer
    
random is a twig function. random(entry.entriesFieldHandle) will retrieve all the entries, then pick a random one. –  Marion Newlevant Jul 12 at 14:40
    
@Marion Is anything bad in it? OP seems to ask about twig... Sorry, not really so experienced :-) –  nicael Jul 12 at 14:43
    
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 Jul 12 at 14:47
    
@Marion You mean if it found an entry? Well, why not... –  nicael Jul 12 at 14:50
1  
@micael yes. If there are no entries, randomEntry.url will give an error –  Marion Newlevant Jul 12 at 14:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.