👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

How to Make a Relative Post Query that is Based on Tags

3 replies
Last updated: May 19, 2022
Hello, a bit complicated query needed. I wanna make a relative post query that is based on tags.
Every post document has a field called
tags
that is an
array
of
strings
.I want to query every post that contains a specific tag
string
. Is that possible in GROQ? Maybe the built in functions can help in this case.
Logic would be:

*[_type=="post" && slug.current!=$slug && (special query here) ][3] { title }
May 19, 2022, 2:13 PM
Pass the tag you’re looking for as a parameter to your query, and use the
in
operator to check its existence in an array.
*[ _type == "post" && slug.current != $slug && $tag in tags ]
May 19, 2022, 2:21 PM
Oh, this easy? 😄
Thank you, it worked!
May 19, 2022, 2:23 PM
Lovely! ❤️
May 19, 2022, 2:23 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?