Build with Sanity MCP in Replit. Prizes up for grabs! Submit by June 14

Dynamic filters with reference to multiple types in Sanity

13 repliesLast updated: Nov 29, 2025

Hello!Not sure if it’s doable but, have any of you guys implemented
dynamic filters and reference multiple types at the same time?
I’m looking for something like the following code but having an array of references instead of a single reference:

{
  title: 'Director',
  name: 'director',
  type: 'reference',
  to: [{type: 'person'}],
  options: {
    filter: ({document}) => {
      // Always make sure to check for document properties
      // before attempting to use them
      if (!document.releaseYear) {
        return {
          filter: 'role == $role',
          params: {role: 'director'}
        }
      }
      
      return {
        filter: 'role == $role && birthYear >= $minYear',
        params: {
          role: 'director',
          minYear: document.releaseYear
        }
      }
    }
  }
}
AI Update

Yes, you can definitely implement dynamic filters and reference multiple types at the same time in Sanity! Your example code is on the right track, but you'll need to apply it to an array of references instead of a single reference field.

Array of References with Dynamic Filter

{
  title: 'Directors',
  name: 'directors',
  type: 'array',
  of: [
    {
      type: 'reference',
      to: [{type: 'person'}],
      options: {
        filter: ({document}) => {
          if (!document.releaseYear) {
            return {
              filter: 'role == $role',
              params: {role: 'director'}
            }
          }
          
          return {
            filter: 'role == $role && birthYear >= $minYear',
            params: {
              role: 'director',
              minYear: document.releaseYear
            }
          }
        }
      }
    }
  ]
}

Multiple Reference Types with Dynamic Filters

If you want to reference multiple document types with different filters for each:

{
  title: 'Crew Members',
  name: 'crewMembers',
  type: 'array',
  of: [
    {
      type: 'reference',
      to: [
        {type: 'person'},
        {type: 'company'}
      ],
      options: {
        filter: ({document}) => {
          if (!document.releaseYear) {
            return {
              filter: '_type in ["person", "company"]',
              params: {}
            }
          }
          
          return {
            filter: '(_type == "person" && birthYear >= $minYear) || _type == "company"',
            params: {
              minYear: document.releaseYear
            }
          }
        }
      }
    }
  ]
}

Key Points

The filter function has access to the parent document, so you can create conditional logic based on any field in your document, just like in your example with releaseYear.

According to the Sanity reference type documentation, the filter option can be either a string or a function. When you provide a function, it's called with an object containing document, parent, and parentPath properties, and it should return an object with filter and params properties. This works perfectly for arrays of references!

Show original thread
13 replies

Was this answer helpful?

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.

Related contributions