
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeYou're correct! GROQ doesn't currently have a built-in random() function or order(random()) capability to query random documents. This is a known limitation of the query language.
However, there are several workarounds you can use to achieve random or pseudo-random document selection:
The most straightforward approach is to fetch more documents than you need and randomize them on the client side:
*[_type == "post"][0...20]Then in your application code, shuffle the results and take the number you need.
You can create a pseudo-random selection by using the document's _id with modulo operations. This gives you different "buckets" of documents:
*[_type == "post" && string::split(_id, "")[0] in ["a", "b", "c"]]This selects documents whose ID starts with certain characters. You can vary which characters you use to get different sets.
Another approach is to rotate which documents you fetch based on the current time:
*[_type == "post"] | order(_createdAt desc)[($seed % count(*))...($seed % count(*) + 5)]Where $seed is a parameter you pass based on the current day/hour/minute to rotate through different documents.
If you're doing any kind of search or filtering, you can combine the score() function with the boost() function in creative ways to vary results, though this isn't truly random.
The lack of a native random() function is a deliberate design choice in GROQ, as true randomization can be challenging to implement efficiently in a distributed query system and can make caching less effective. For most use cases, the client-side randomization approach works well and keeps your queries cacheable and performant.
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store