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.io – build remarkable experiences at scale

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

Categorized in