
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeYou're very close! The issue is that you need to pass the actual subServices array values to the GROQ filter as parameters. Here's the corrected version:
export const Service = defineType({
name: 'service',
title: 'Service',
type: 'document',
fields: [
defineField({
name: 'subServices',
title: 'Sub Services',
type: 'array',
of: [
{
type: 'reference',
to: [{ type: 'service' }],
options: {
filter: ({ document }) => {
const subServiceIds = (document.subServices || [])
.map(ref => ref._ref)
.filter(Boolean);
return {
filter: '!(_id in $subServiceIds)',
params: { subServiceIds }
}
}
}
}
],
})
]
})The key differences from your code:
document.subServices to get an array of _ref values from the reference objects$subServiceIds) rather than trying to reference the field directly in the query string|| [] fallback and filter(Boolean) to handle cases where the array doesn't exist yet or contains undefined valuesThe GROQ filter string !(_id in $subServiceIds) means "exclude documents whose _id is in the subServiceIds array", which is exactly what you want.
This pattern works because the filter function receives the current document state, allowing you to dynamically adjust what references are available based on what's already been selected. The dynamic filter will re-evaluate each time the subServices array changes, keeping the available options up to date.
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