How to select() from referenced values with multiple document types in GROQ?

5 repliesLast updated: Nov 29, 2025

boiling this down to the simplest form, is there a way I can
select()
from referenced values using GROQ?I have a reference which could refer to two possible document types. This query totally works – but it doesn’t seem like the “best” way to do it, and I’m not sure if there would be some unexpected behaviour outside of this isolated example, or in a more complicated case?

*[_type == "story" && !(_id in path('drafts.**'))] | order(publicationDate desc) {
	"references": references[] {
  	"title": reference-> name,
  	"title": reference-> title,
  }
}
AI Update

Yes! 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.

Show original thread
5 replies

Was this answer helpful?

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.

Related contributions