How to get slug from a reference in Sanity?

11 replies
Last updated: Jan 28, 2026
Hey everyone, I think this is an easy one; how do you get a slug from a reference? I have a reference to another page I want to act as a link to another page, but I only get the
_ref
returned and not the
slug
AI Update

Perfect! 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 (->).

Basic Solution

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.

Getting Just 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
}

For Arrays of References

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.

Show original thread
11 replies

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.

Was this answer helpful?