Joint session with Vercel: How to build intelligent storefronts (May 15th)

Backlinks Query

Get all content that references the current document

By David Witt


Get Post

*[_type == "post" && slug.current == $slug][0] {
  ...,
  "backlinks": *[references(^._id) && isVisible == true]{ 
    title,
    _type,
    slug,
    description,
  }
}

Backlinks are a way to show a document that links to the current document. All links in Sanity are bidirectional in nature, but surfacing these links through GROQ is not super duper obvious.

GROQ has a valuable ^ operator that refers to the enclosing document.

This allows you to use the current document's values in a child query. In this example, documents can link to each other via an Internal Link. These links are queryable with the references() function.

Check out the Space Madness docs to see backlinks in action.

Contributor

David Witt

Developer advocate, front-end engineer, dubious philosopher

United States

Visit David Witt's profile