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

How to Get a Slug From a Reference

11 replies
Last updated: Nov 7, 2021
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
Oct 29, 2021, 1:13 PM
in your groq query you can do something like this:

{"slug" : reference->slug.current}
Oct 29, 2021, 2:39 PM
Thanks
user H
— what if you're just returning the entire query though? I have a load of blocks for building content and don't want to have to type all of them out
Nov 7, 2021, 7:03 PM
Hey
user S
you can just use a spread operator there so

{…, "slug" : reference->slug.current}
Nov 7, 2021, 7:10 PM
Thanks
user H
, I actually just stumbled upon this as you posted it but thanks so much for the fast reply
Nov 7, 2021, 7:13 PM
groq really is awesome
Nov 7, 2021, 7:13 PM
It's great isn't it!
Nov 7, 2021, 7:14 PM
Sorry
user H
one last question, I have another set of data I need to grab a reference from but it's nested and a bit fuzzy...
How would I resolve these types of references? Something like

[].blocks[].link->slug.current

Nov 7, 2021, 7:21 PM
I'm new to groq so just trying to understand
Nov 7, 2021, 7:21 PM
Ah, got it...
  *[_type == "page" && slug.current == 'values-and-beliefs'] {
    blocks[] {"link": link->slug.current, ...},
    ...
  }
Nov 7, 2021, 7:28 PM
Ah, got it...
  *[_type == "page" && slug.current == 'values-and-beliefs'] {
    blocks[] {"link": link->slug.current, ...},
    ...
  }
Nov 7, 2021, 7:28 PM
Thanks again for your help
Nov 7, 2021, 7:28 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 fetch an array of images using groq in Sanity.ioMay 21, 2023

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