Adding multiple categories to a product in Sanity.io
To assign multiple categories to a product in Sanity, you'll want to use an array of references in your product schema. This allows a single product (like a shirt) to belong to multiple categories simultaneously.
Here's how to set this up:
export default {
name: 'product',
type: 'document',
title: 'Product',
fields: [
{
name: 'title',
type: 'string',
title: 'Title'
},
{
name: 'categories',
type: 'array',
title: 'Categories',
of: [
{
type: 'reference',
to: [{type: 'category'}]
}
]
},
// ... other product fields
]
}And your category schema would look like:
export default {
name: 'category',
type: 'document',
title: 'Category',
fields: [
{
name: 'title',
type: 'string',
title: 'Title'
},
{
name: 'slug',
type: 'slug',
title: 'Slug',
options: {
source: 'title'
}
}
]
}With this setup, when you edit a product in Sanity Studio, you'll see a "Categories" field where you can add multiple category references. Your shirt can then be in both "Featured" and "Shirts" categories at the same time.
To query products with their categories using GROQ:
*[_type == "product"] {
title,
"categories": categories[]-> {
title,
slug
}
}Or to find all products in a specific category:
*[_type == "product" && references(*[_type=="category" && slug.current == "featured"]._id)]This approach is mentioned in the Product Information Management article, where products can be "grouped into categories through reference documents." It's a flexible pattern that scales well as your product catalog grows.
Sanity – Build the way you think, not the way your CMS thinks
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.