See how we built our intranet. Live + Q&A, June 30

GROQ: Filter product attributes by category's attribute references

6 repliesLast updated: Nov 29, 2025

Hello. I'm in need of some (hopefully) quick GROQ help. My documents of type "category" have a list of references to documents of type "attribute". My documents of type "product" has a reference to a category and also have a list of "attributes". How can I make it so my query fetches only the product attributes that its category lists?To visualize, the query would theoretically be something like
*[_type == "product"] { "attrs": attributes[attrRef in @.category.attributeRefs] }
, but this doesn't work. Any ideas?
AI Update

Great question! You're looking to filter an array of references based on whether those references match certain criteria. The key is using the @ operator to reference the current element within an array filter.

Here's the solution for your use case:

*[_type == "product"] {
  "attrs": attributes[@._ref in ^.category->attributeRefs[]._ref]
}

However, if you want to filter based on the actual attribute values (not just the references), you'll want to use this pattern:

*[_type == "product"] {
  "attrs": attributes[(@->._id in ^.category->attributeRefs[]._ref)]->
}

The most robust solution based on this answer from the Sanity community would be:

*[_type == "product"] {
  "attrs": attributes[(@._ref in ^.category->attributeRefs[]._ref)]->,
  // ... other fields
}

Key concepts here:

If you want to go a step further and ensure you only get products that have at least one matching attribute, you can add a filter at the document level:

*[_type == "product"] {
  "attrs": attributes[(@._ref in ^.category->attributeRefs[]._ref)]->,
  _id
}[length(attrs) > 0]

Note: This pattern requires API version v2021-03-25 or later to work properly with the @ operator in array filters.

Show original thread
6 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