GROQ: Filter blog posts by referenced tag names

6 replies
Last updated: Apr 13, 2021
Hello, I am trying to work with GROQ to get some information.For each of my "blog post" document types, there are 3 "tag" document types referenced. I would like to filter my "blog post" query based on its referenced "tag" names. (edited) 
If anyone has any ideas on how I might make this work, I'm all ears. Here is my current implementation:

*[_type == "post"] // && tags[]->tag == "Tutorial" (this doesn't work)
{
  'slug': slug.current, 
  title, 
  'date': _createdAt, 
  subtitle,
  'tags': tags[]->{tag, 'color': tagColor.hex, description, 'slug': slug.current},
} 
| order(_createdAt desc)
I posted in
groq but still haven't gotten any feedback
AI Update

I can help you filter your blog posts by referenced tag names! The key is understanding how to query through references in GROQ arrays.

Your commented-out filter is close, but needs a slight adjustment. Here are the working approaches:

Option 1: Using the in operator (recommended)

*[_type == "post" && "Tutorial" in tags[]->tag]
{
  'slug': slug.current, 
  title, 
  'date': _createdAt, 
  subtitle,
  'tags': tags[]->{tag, 'color': tagColor.hex, description, 'slug': slug.current},
} 
| order(_createdAt desc)

The in operator checks if "Tutorial" exists anywhere in the array of dereferenced tag names. This is the most readable and performant approach.

Option 2: Using count() for existence check

*[_type == "post" && count(tags[tag == "Tutorial"]) > 0]

This filters the tags array first, then checks if any matches exist.

For multiple tag filters:

If you want posts that have ANY of several tags (OR logic):

*[_type == "post" && count(tags[]->tag in ["Tutorial", "Guide", "How-to"]) > 0]

If you want posts that have ALL specified tags (AND logic):

*[_type == "post" 
  && "Tutorial" in tags[]->tag 
  && "Advanced" in tags[]->tag
]

The -> operator dereferences the tag document, and then you can access its fields like tag. The [] after tags means "for all items in this array," so tags[]->tag gives you an array of all tag names from the referenced documents.

According to the query optimization documentation, it's best practice to filter as early as possible in your query pipeline, which is exactly what you're doing by putting this condition in your initial filter. This approach ensures better performance as your dataset grows.

Show original thread
6 replies
For checking against arrays, it's best to use the
in
operator.
Something like this might be what you're looking for (I typically would key off of the _id instead of the tag string, but the tag string is nicely readable)


*[_type == "post" && "Tutorial" in tags[]->tag]

That will see if the string "Tutorial" is in the array that's generated by tags[]->tag (which will be an array of all the tag strings).
If you use the id instead (which will protect agains the tag string changing), you'd modify the filter to look more like this

*[_type == "post" && "some-long-id-string" in tags[]._ref]
(you'd no longer need to "de-reference" the tags, since the
_ref
matches the Tag document ID
For checking against arrays, it's best to use the
in
operator.
Something like this might be what you're looking for (I typically would key off of the _id instead of the tag string, but the tag string is nicely readable)


*[_type == "post" && "Tutorial" in tags[]->tag]

user T
oh i see! i was essentially trying to compare two arrays. so is there a way to check if multiple tags match like
["Tutorial", "Azure"] in tags[]->tag
Unfortunately, there's no array intersections, so the syntax would be:
("Tutorial" in tags[]->tag || "Azure" in tags[]->tag) // OR
("Tutorial" in tags[]->tag && "Azure" in tags[]->tag) // AND
Depending on if you want AND or OR
Thank you!!

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.

Was this answer helpful?