
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeYes! GROQ has a select() function that's perfect for this exact scenario. It's a much cleaner way to handle conditional field selection from references that could point to different document types.
Instead of your current approach (which does work but relies on duplicate field names silently overwriting each other), you can use select() like this:
*[_type == "story" && !(_id in path('drafts.**'))] | order(publicationDate desc) {
"references": references[] {
"title": select(
defined(reference->name) => reference->name,
defined(reference->title) => reference->title
)
}
}The select() function evaluates conditions in order and returns the first truthy value. Each condition follows the pattern condition => value.
If you know the specific document types, you can be even more explicit:
*[_type == "story" && !(_id in path('drafts.**'))] | order(publicationDate desc) {
"references": references[] {
"title": select(
reference->_type == "typeA" => reference->name,
reference->_type == "typeB" => reference->title,
"Untitled" // optional fallback
)
}
}Why this is better than your current approach:
The select() function stops evaluating as soon as it finds a matching condition, so it's also efficient. You can add as many conditions as you need, and always include a fallback value at the end for safety.
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