Get random page via API, e.g. from a pool of testimonials

Problem

You have a pool of pages, e.g. quotes/testimonials, and want to display a random one.

Solution

A little example how to pull a random page:

$quote = $pages->find("template=quote, include=hidden, sort=random, limit=1")->first();

if($quote->id) {

    $q = $quote->quote; // example field
    $a = $quote->author; // example field

    echo "<blockquote>\n";
    echo "  <p>$q</p>\n";
    echo "  <footer>— <cite>$a</cite></footer>\n";
    echo "</blockquote>\n";

}

Resources

Version

1.2.0

View/correct recipe on GitHub

https://github.com/processwire-recipes/Recipes/blob/master/get-random-page.md

Author(s)