Filter expanded references by properties in Sanity GROQ query
Looking at your query, the issue is that you're trying to filter on the reference objects themselves before dereferencing them. In GROQ, you need to filter after dereferencing to access the properties of the referenced documents.
Here are a few approaches to solve this:
Option 1: Filter after dereferencing (Recommended)
*[_id == "allRecipesPage"]{
editorsPicks {
"recipes": recipes[]-> {
${recipeTeaserGroq}
}[!defined(releaseDate) || releaseDate <= $currentDate],
},
}The key is moving the filter to after the -> dereference operator. This way you're filtering on the actual recipe documents, not the reference objects.
Option 2: Filter inline during dereferencing
*[_id == "allRecipesPage"]{
editorsPicks {
"recipes": recipes[]->{
${recipeTeaserGroq}
}[!defined(releaseDate) || releaseDate <= $currentDate],
},
}This is essentially the same as Option 1 - the filter [!defined(releaseDate) || releaseDate <= $currentDate] is applied to the dereferenced results.
Option 3: Use a subquery approach
*[_id == "allRecipesPage"]{
editorsPicks {
"recipes": recipes[]._ref in *[
_type == "recipeArticle"
&& (!defined(releaseDate) || releaseDate <= $currentDate)
]._id => recipes[]->{
${recipeTeaserGroq}
},
},
}Option 1 or 2 are the cleanest approaches - just dereference first with ->, then apply your filter to the dereferenced documents. The filter sees the full recipe document properties after the dereference happens.
The pattern is: array[]->{projection}[filter] where the filter operates on the dereferenced documents, not on the reference objects themselves. This is documented in the Query Cheat Sheet under the "Joins" and "Object Projections" sections.
Show original thread1 reply
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.