
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storePerfect! This is a common situation when working with references in Sanity. By default, references only return the _ref field (which is just the document ID), but you can easily get the full referenced document including the slug using the reference access operator (->).
If you have a reference field like linkedPage, you can dereference it in your GROQ query using ->:
*[_type == "yourDocumentType"] {
...,
linkedPage->
}This will replace the reference with the entire referenced document, including the slug.
If you only need the slug (not the entire referenced document), you can project just that field:
*[_type == "yourDocumentType"] {
...,
linkedPage->{
slug
}
}Or even more concisely, access the slug directly:
*[_type == "yourDocumentType"] {
...,
"pageSlug": linkedPage->slug.current
}If you have an array of page references, you can dereference all of them:
*[_type == "yourDocumentType"] {
...,
relatedPages[]->{
slug
}
}The reference access operator works by internally executing a subquery to fetch the referenced document and merge it into your results. Just keep in mind that each -> does perform an additional query, so be mindful of performance when dereferencing large arrays or deeply nested references.
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