Dynamic filter for reference list based on product reference not working

13 replies
Last updated: Jan 28, 2026
Hey all! Real quick question for you. I'm trying to filter a reference list with a dynamic filter. This is where I'm currently at but it doesn't seem to do anything right now:
filter: ({ document }) => {
  const ref = document.content?.product?._ref
  if (!ref) return
  return {
    filter: 'product._ref == $product',
    params: {
      product: ref,
    },
  }
},
The document that this is being run within has a field
content.product
which references a
product
, and the document type I am trying to filter here should also reference the same
product
. You can't use
console.log
within filter functions, so I have no way to be able to inspect what
document
contains. I've tried a number of different ways of going about this, including checking for the presence of
document.content
and
document.content.product
first, but nothing seems to make a difference here.
Any ideas, anyone?
AI Update

Looking at your code, the issue is likely that you're trying to access nested properties that may not exist yet when the filter function runs. Here are a few things to check:

The Problem:

The document object in filter functions represents the current document being edited, so document.content?.product?._ref should work, but you need to ensure the document structure matches and that you're filtering correctly.

Solution:

Based on the Sanity documentation on reference filters, here's a corrected version:

filter: ({ document }) => {
  const productRef = document?.content?.product?._ref
  
  // Make sure we have a product reference before applying filter
  if (!productRef) {
    return {} // Return empty object to show all results when no product selected
  }
  
  return {
    filter: 'product._ref == $productRef',
    params: {
      productRef: productRef,
    },
  }
}

Key points to understand:

  1. Always check for existence - Your optional chaining looks good, but make sure you're checking at each level
  2. Return an empty object {} when there's no filter to apply (not undefined or nothing)
  3. The filter applies to target documents - If you're filtering a reference field that points to some document type, the GROQ filter runs against those target documents, not your current document
  4. Parameter naming - I changed product to productRef in params to be clearer, but the actual name doesn't matter as long as it matches in both the filter string and params object

Debugging tips:

Since you can't use console.log inside the filter function, try these approaches:

  1. Check your schema structure - Make sure the field path content.product actually exists in your document
  2. Verify the target documents - The documents you're trying to reference should also have a product reference field that you're filtering against
  3. Test the GROQ filter separately - Try running your GROQ query in Vision or the GROQ playground to verify it works
  4. Use the document inspector - In Studio, use the "Inspect" option on documents to see their actual structure

If you're still having issues, it would help to know:

  • What document type has this filter?
  • What document types are you referencing (the to field)?
  • Do the target documents have a product reference field?
Show original thread
13 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.

Was this answer helpful?