✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

How to filter a reference based on a value in the same document in Sanity.io.

2 replies
Last updated: Mar 13, 2024
Hello, how can I filter a reference depending on a value in the same document?I'm working on the website for a custom furniture factory. I have two documents, 'Project' and 'Service', both can belong to a certain type of service (kitchen, bathroom, bedroom or other).
I need the 'project selection' option within the service to be filtered depending on which service it is, so in the "Service" document I have to choose a 'furnitureType' first and then I have to chose a project that has the same value for 'furnitureType'
Does anyone know how to do that?
Mar 13, 2024, 12:07 AM
You’ll need to do something along the lines of:
`_type == 'project' && category == $category`
then your
params
would need to be:
{ category: document.furnitureType }
Mar 13, 2024, 4:39 PM
If you're trying to select two or more types defined by a schema, you can use this method. The first image shows how to select. In the second, demonstrate how you will select, and the third shows the list of selected types for further selection.
Mar 13, 2024, 9:20 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?