Selecting a Random Entry
Problem
Selecting a random entry.
Solution
You can retrieve a random entry by setting the sort order to RAND()
, and the limit to 1:
{% set entry = craft.entries.order('RAND()').limit(1) %}
As with any other craft.entries
request, you can optionally filter the returned entries by section, status, relationships, and so forth.
For example, the following code retrieves a random entry from the “products” section:
{% product = craft.entries.section('products').order('RAND()').limit(1) %}
Submitted by Jacob Graf on 1st February, 2017