🎤 Builder Talk: The Story Behind Lady Gaga’s Digital Experience – Register now

Querying references inside a Portable text field in Sanity

8 replies
Last updated: Jan 11, 2022
Following the field level translation documentation, I have a content object that contains the languages as keys. Normally I can access any field like this.

"content": coalesce(content[$lang], content.en)

This content is a Portable text field, and I want to query the current language or fallback, but I want to be able to reach the references inside that object, like referenced images.

Say I want to access this asset of
pageImage
that is a block inside `content`:

_type == "pageImage" => {

...,

"alt": coalesce(alt[$lang], alt.en),

"imageBlock": imageBlock {

"asset": asset->url,

"dimensions": asset->metadata.dimensions

},

},

This doesn’t work:


"content": coalesce(content[$lang], content.en) {

....,

_type == "pageImage" => {

...,

alt,

"imageBlock": imageBlock {

"asset": asset->url,

"dimensions": asset->metadata.dimensions

},

},

}

How would you query that?
Jan 11, 2022, 4:31 PM
Hi! Using
coalesce()
in this way should work fine. Which API version are you on?
Jan 11, 2022, 4:55 PM
The api version is “2021-03-25”
If I only query this

"content": coalesce(content[$lang], content.en) {

...,

}

I get null. Should I change the api version to something else?
Jan 11, 2022, 5:21 PM
Odd. That API version should work.
Jan 11, 2022, 5:31 PM
Can you do just:
"content": coalesce(content[$lang], content.en)
and see?

Also, if that returns null, how about just:

"content": coalesce(content[$lang])
Jan 11, 2022, 5:32 PM
"content": coalesce(content[$lang], content.en)
works well. the problem is that I cant access the reference of the images inside that object if I do only that.
Jan 11, 2022, 6:01 PM
In the output of content I will get
"imageBlock": {
        "_type": "image",
        "asset": {
          "_ref": "image-e54e00da4b0c81d18aea3260fd7a551b5f914121-702x490-gif",
          "_type": "reference"
        }
How do I access that reference?
Jan 11, 2022, 6:01 PM
I found the solution! After coalesce() I should query inside the array, not an object key.

"content": coalesce(content[$lang], content.en)[] {

...,

_type == "image" => {

...,

alt,

"imageBlock": imageBlock {

"asset": asset->url,

"dimensions": asset->metadata.dimensions

},

}

}
Jan 11, 2022, 6:08 PM
Ah, for some reason I thought
content[...]
was an object. 🙂 Great!
Jan 11, 2022, 6:13 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
Hey all - what’s the quickest way to batch delete all documents of a certain type? It looks like the `sanity` cli tool allows...Jan 29, 2021
Rendering nested block contentJan 18, 2021
In the tutorial, there is samples of what the API returns as JSON. Is there a way to view these ?Jan 11, 2021
Warning: Data for Page Exceeds the Threshold of 128 kBAug 17, 2022
Custom Document Views - Is it Even Possible to Use the Structure Builder?Apr 21, 2022
How to get the number of nested/children components in a GROQ query.Jan 19, 2024
Issue with fetching data from an array of references and non-references in a groq query.Dec 14, 2023
GraphQL query resolving references on a _raw field in GatsbyMay 17, 2022
GROQ query for getting references from arrays in another array in Sanity.ioApr 9, 2020
How to populate a list of values in a Sanity schema using a separate fileSep 6, 2021

Related contributions

Turbo Start Sanity
- Template

The battle-tested Sanity template that powers Roboto Studio's websites

Go to Turbo Start Sanity

Schema UI - Next.js Sanity Starter
- Template

A Next.js starter template with Next.js 15, Tailwind CSS, shadcn/ui, and Sanity CMS with Live Editing. Get production-ready React components with matching Sanity schemas and queries. Build dynamic pages faster while keeping full control over customization.

Serge Ovcharenko
Go to Schema UI - Next.js Sanity Starter

The Swaddle
- Made with Sanity

A new brand identity to represent a more mature company, to signify The Swaddle’s evolution from publisher to production house, combined with an easier to navigate platform that can surface multiple content types - drawing readers through The Swaddle’s content offering.

Nightjar
Go to The Swaddle