Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

When Referencing another Schema, how to Extract the URL from that Referenced Post?

4 replies
Last updated: Jul 20, 2022
Hey everyone, when referencing another schema (
type: "reference"
), how can I extract the URL from that referenced post? At the moment the output only provides me with the _ref and _type, which isn't very useful...


    {
      name: "linkToNews",
      title: "Lien au publication de nouvelles",
      description:
        "Créer un lien vers un article de presse contenant plus d'informations sur les dates limites de soumission.",
      type: "reference",
      to: { type: "nouvelles" },
    },
Jul 20, 2022, 2:55 PM
You could expand it using GROQ:
*[_type == 'yourdoc']{ name, linkToNews-> }
see the documentation on joins here: https://www.sanity.io/docs/groq-joins
Jul 20, 2022, 2:59 PM
nice, thanks! That worked and I'm able to query all the data from
linkToNews
, but how would I just extract the
slug
rather than all the data from a given
news
post?
Jul 20, 2022, 3:07 PM
Cool. Here's what you could do:
*[_type == 'nouvelles']{
  name, 'slug': linkToNews->slug
}
Jul 20, 2022, 3:10 PM
awesome, cheers!!
Jul 20, 2022, 3:11 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?