Query referenced tag title in GROQ array filter
Looking at your query, you're on the right track! The issue is that when you filter with tags[]-> title == "NextJS", you're checking if any tag matches, but then you need to actually project/return the title in your results.
Here are a few approaches depending on what you want to achieve:
If you want just the matching tag titles:
*[_type == "contents" && "NextJS" in tags[]->title] {
"matchingTags": tags[]->title
}If you want only the "NextJS" title specifically:
*[_type == "contents" && "NextJS" in tags[]->title] {
"nextjsTag": tags[]->{title}[title == "NextJS"][0].title
}If you want to filter the tags array to only include matching references:
*[_type == "contents" && "NextJS" in tags[]->title] {
_id,
"filteredTags": tags[]->{ title }[title == "NextJS"]
}The key things to understand here:
tags[]->titledereferences all tag references and gets their titles as an arraytags[]->{title}dereferences and projects each tag, giving you more control over filtering- The filter condition checks if the value exists, but you still need to project what you want in the result using projections
The most common pattern for your use case would be:
*[_type == "contents" && "NextJS" in tags[]->title] {
...,
"tagTitles": tags[]->title
}This gives you all content documents that have a "NextJS" tag, and returns an array of all tag titles (including "NextJS") for each document. The ... spreads all other fields from the content document, while "tagTitles" creates a new field with the dereferenced tag titles.
Show original thread5 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.