πŸ‘€ Our most exciting product launch yet πŸš€ Join us May 8th for Sanity Connect

Backlinks Query

By David Witt

Get all content that references the current document

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

Other schemas by author