
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeBased on what you're describing, you have a hierarchical structure where sections reference chapters, and chapters reference modules. There isn't a built-in "get all parent documents" method in Sanity Studio, but you can absolutely achieve this using GROQ queries with reference dereferencing.
First, make sure your schema uses reference fields to establish the parent relationships:
// section.js
{
name: 'section',
type: 'document',
fields: [
{
name: 'slug',
type: 'slug'
},
{
name: 'chapter',
type: 'reference',
to: [{type: 'chapter'}]
}
]
}
// chapter.js
{
name: 'chapter',
type: 'document',
fields: [
{
name: 'slug',
type: 'slug'
},
{
name: 'module',
type: 'reference',
to: [{type: 'module'}]
}
]
}When querying for a section, you can use GROQ's -> operator to follow references and get the parent slugs:
*[_type == "section" && slug.current == $sectionSlug][0] {
slug,
"chapterSlug": chapter->slug.current,
"moduleSlug": chapter->module->slug.current
}This query:
chapter reference with -> to get the chapter's slug-> to follow the chapter's module referenceYou can also get the full parent objects if you need more than just slugs:
*[_type == "section" && slug.current == $sectionSlug][0] {
slug,
chapter-> {
slug,
module-> {
slug
}
}
}If you need to access this information within the Studio (for example, in a custom component or plugin), you can use the Sanity client to run these queries:
import {useClient} from 'sanity'
const client = useClient({apiVersion: '2024-01-01'})
const result = await client.fetch(
`*[_type == "section" && _id == $id][0] {
slug,
"chapterSlug": chapter->slug.current,
"moduleSlug": chapter->module->slug.current
}`,
{id: documentId}
)The key insight from the reference fields documentation is that references in Sanity are bidirectional when queried, meaning you can traverse them efficiently in either direction using GROQ's dereferencing operator (->).
This approach gives you full control over which parent information you need and avoids storing redundant slug data in your documents, keeping your content normalized and easier to maintain.
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store