GROQ query for getting references from arrays in another array in Sanity.io
You're running into the classic GROQ challenge of checking if any element in one array matches any element in another array! The error No function in() defined for arguments (array, array) happens because the in operator can't directly compare two arrays.
The solution is to use the count() + @ pattern to check for array overlap. Here's how to fix your query:
*[_type == 'articleList'][0] {
"articlesToShow": *[
_type == 'article'
&& count(tags[]._ref[@ in ^.articleTags[]._ref]) > 0
]
}How this works:
tags[]._refprojects your article's tag references into a simple array like["tag1", "tag2"]- The
[@ in ^.articleTags[]._ref]filters this array to only elements that exist in the parent'sarticleTags count(...)counts how many matches there are> 0means "at least one tag matches"
The ^ operator accesses the parent scope (your articleList document) from within the subquery.
Alternative approach if you want it more explicit:
*[_type == 'articleList'][0] {
articleTags,
"articlesToShow": *[
_type == 'article'
&& count(tags[@._ref in ^.^.articleTags[]._ref]) > 0
]
}Note the double ^.^. - this is because tags[query] creates its own closure, so you need to go up two levels to reach the articleList document.
This pattern is one of the trickier parts of GROQ, but it's super powerful once you get the hang of it! The community thread I found shows several examples of this technique in action, including a detailed breakdown for matching related posts by tags.
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.