
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeYou're right that GROQ doesn't currently support random sorting directly! This is a feature that's been requested since 2019 and is still being considered by the Sanity team. Here are the workarounds available:
The most common approach is to fetch your documents and shuffle them client-side:
*[_type == "post"]Then use JavaScript to shuffle the results:
const shuffled = results.sort(() => 0.5 - Math.random()).slice(0, 10);This works but means fetching more data than you need, which isn't ideal for performance.
Your instinct about using a random field is actually a solid workaround! Add a randomOrder field to your documents and update it periodically:
// In your document schema
{
name: 'randomOrder',
type: 'number',
hidden: true
}Then query with:
*[_type == "post"] | order(randomOrder asc)[0..9]You can update these values with a cron job or Sanity Function that runs periodically. This gives you cacheable "random" results that change on your schedule.
A more sophisticated approach mentioned in the GitHub discussion is to store a random index (0-1) in each document, then generate a random number client-side and fetch documents with indices greater than that number:
*[_type == "post" && randomIndex > $randomNumber] | order(randomIndex asc)[0..5]This reduces the data transferred but still requires some client-side randomness.
The main challenge is caching. Random queries are essentially uncacheable, which would hurt performance at scale. One proposed solution is a seed parameter where the same seed always returns the same "random" order, allowing caching while still providing variability.
The Sanity team has acknowledged this feature request and is considering it, but there's no timeline yet. Your cron job approach is probably the most practical solution for now!
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