
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeYes, absolutely! You can filter out already-selected references from the dropdown so editors only see options that haven't been added yet. This is a common need and there's a clean solution using the options.filter property on your reference field.
Here's how to set it up. The key is to place the filter on the reference field inside your array, not on the array itself:
{
title: 'Services',
name: 'services',
type: 'array',
of: [
{
type: 'reference',
to: [{type: 'service'}],
options: {
filter: ({document}) => {
// Get all currently selected service IDs
const selectedServices = (document.services || [])
.map(service => service._ref)
.filter(Boolean)
return {
filter: '!(_id in $selectedServices)',
params: {
selectedServices
}
}
}
}
}
]
}This works by:
_ref values from all currently selected items in your array_id matches any of those references.filter(Boolean) removes any undefined values to prevent errorsIf you're using this pattern across multiple fields, you can create a utility function. Here's a reusable approach from the Sanity community:
// utils/getFilter.js
export const getFilter = (document, field) => {
const existingEntries = (document[field] || [])
.map(entry => entry._ref)
.filter(Boolean)
return {
filter: '!(_id in $existingEntries)',
params: {
existingEntries
}
}
}Then in your schema:
import { getFilter } from '../utils/getFilter'
{
title: 'Services',
name: 'services',
type: 'array',
of: [
{
type: 'reference',
to: [{type: 'service'}],
options: {
filter: ({document}) => getFilter(document, 'services')
}
}
]
}This way, once Orthopedics is selected, it won't appear in the dropdown anymore when you go to add another service. The filter updates dynamically as you add or remove items from the array.
You can read more about dynamic reference filters in the Sanity docs and see additional discussion in this community thread about filtering reference arrays.
Happy Friday! 🎉
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