How to resolve references in PortableText array using GROQ?

9 repliesLast updated: Nov 29, 2025

Hello! Is there some GROQ query where references in a PortableText array of blocks are also resolved? I haven't found a way to address them in my projection.
AI Update

Yes! You can resolve references within PortableText arrays using the @ operator combined with conditionals. The @ operator refers to the root value of the current scope, which is perfect for handling mixed content where some items are references and others aren't.

Here's the pattern you'll want to use:

*[_type == "yourDocType"] {
  content[]{
    ...,
    _type == 'yourReferenceType' => {
      'resolved': @->
    }
  }
}

For example, if you have a PortableText array with custom reference blocks mixed with regular text spans, you can do:

{
  "content": content[]{
    ...,
    _type == 'orderReceiptPlaceholderRef' => {
      'referenceData': @->
    }
  }
}

The key points:

This approach works because:

You can find more details about GROQ conditionals in the query cheat sheet and learn about the reference access operator (->) in the GROQ joins specification.

Show original thread
9 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