How to select multiple items from a reference field dropdown in Sanity?
Yes! To select multiple items from a dropdown, you need to change your field from a single reference type to an array of references. Here's how to modify your schema:
{
name: "category",
title: "Category",
type: "array", // Changed from "reference" to "array"
of: [
{
type: "reference",
to: { type: "category" }
}
],
inputComponent: conditionalFields,
options: {
condition: (document, parent) => parent.articleType != "shortStories"
}
}The key changes are:
- Change
typeto"array"- This makes the field hold multiple items - Add an
ofproperty - This defines what types can be in the array (in your case, references to category documents)
This will give you a multi-select interface where you can add multiple category references. Users can click to add categories, reorder them via drag-and-drop, and remove them individually.
The array field type in Sanity is specifically designed for storing orderable collections of items, and it's the standard way to handle multiple selections. Each selected item automatically gets a unique _key property that Sanity uses to track items.
If you need to query these multiple references later in GROQ, you can use array operations like:
*[_type == "yourDocType"] {
title,
"categories": category[]->title
}The []-> syntax dereferences all items in the array, giving you the full category documents.
Show original thread13 replies
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.