🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

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
that is an
.I want to query every post that contains a specific tag
. 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
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?