Query issue with returning a specific collection in a Sanity.io Slack thread.
Based on the references() function documentation and reference access operator documentation, both -> (dereference operator) and references(^._id) are valid GROQ operators that should work together in the same filter. The documentation confirms that ^ correctly refers to the parent scope in subqueries, and there's no documented limitation preventing these operators from being combined.
However, there's an important detail about how the dereference operator works: according to the documentation, when you use partner->slug.current, the -> operator "internally executes a subquery to fetch the complete document with the matching _id". This means each -> operation is effectively performing an additional query lookup.
The most likely explanation for why your first query returns no results is that you're over-constraining the filter. When you combine:
&& partner->slug.current == $partnerSlug
&& references(^._id)You're requiring that the collection document satisfies BOTH conditions simultaneously. If your collection's partner reference structure or the way references() checks for the post ID doesn't match exactly what you expect, the combined filter will fail to return results.
Practical debugging approach:
First, verify what's actually in your collection document. Run this diagnostic query:
*[_type == 'collection' && slug.current == $collectionSlug][0]{
_id,
partner,
"partnerRef": partner._ref,
"partnerSlug": partner->slug.current,
posts
}Then test each condition individually within your subquery to see which one is failing:
*[_type == 'post' && slug.current == $postSlug && partner->slug.current == $partnerSlug] {
"testReferences": *[_type == 'collection'
&& slug.current == $collectionSlug
&& references(^._id)]{_id},
"testPartner": *[_type == 'collection'
&& slug.current == $collectionSlug
&& partner->slug.current == $partnerSlug]{_id},
"testBoth": *[_type == 'collection'
&& slug.current == $collectionSlug
&& partner->slug.current == $partnerSlug
&& references(^._id)]{_id}
}Alternative approach using reference comparison:
Instead of dereferencing in the filter (which performs a subquery), compare the reference IDs directly:
"collection": *[_type == 'collection'
&& slug.current == $collectionSlug
&& references(^._id)
&& partner._ref == ^.partner._ref
]{
...,
posts[]->{
...,
images[]{..., asset->{...}}
},
}[0]This avoids the dereference operator in the filter condition and directly compares the stored reference IDs, which should be more reliable and performant.
The documentation doesn't indicate any technical limitation with combining these operators, so the issue is most likely with how your data is structured or how the conditions are being evaluated against your actual document structure. The diagnostic queries above should help you identify exactly which condition is failing.
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.