Discussion about issues with the Orderable Document List plugin and querying in Sanity Studio
Great question! You've actually discovered a common gotcha with GROQ queries and ordering fields. The solution you found with Geoff's help is exactly right, and it's worth explaining why this happens.
When you use the order() function in GROQ, the ordering happens on the backend, but GROQ doesn't automatically include the field you're ordering by in your projection. This is particularly important with the Orderable Document List plugin, which uses a special orderRank field to maintain custom ordering.
The Problem
The orderRank field exists in your documents and GROQ can use it for sorting, but if you don't explicitly include it in your projection, it won't be returned in the results. This can cause issues with:
- Live queries: When the order changes, the query re-runs and returns results in the new order, but without
orderRankin your projection, you might not see consistent ordering - Caching layers: Some systems cache based on the returned data structure, and without
orderRank, they can't detect that ordering has changed
The Solution
You need to include the orderRank field (or whatever ordering field your schema uses) in your projection, not just in the order() function:
// ❌ Won't reliably reflect order changes
*[_type == "myDocument"] | order(orderRank) {
_id,
title,
// orderRank not included
}
// ✅ Properly includes ordering field
*[_type == "myDocument"] | order(orderRank) {
_id,
title,
orderRank // Include this!
}This ensures that:
- The ordering field is part of your returned data structure
- Changes to document order are reflected in the actual field values
- Any caching or change detection mechanisms can properly identify when order has changed
Why This Matters
As noted in the GROQ pipeline components documentation, the order component sorts array elements, but projections define which fields to return. These are separate operations, and the order component doesn't automatically modify your projection to include the ordering field.
Thanks for posting your solution as an edit—this will definitely help others who run into the same issue with the Orderable Document List plugin or any custom ordering fields!
Sanity – Build the way you think, not the way your CMS thinks
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.