The Sanity engineering blog is live. Deep dives from our engineers.

Sanity query filter by category or tags (Groq Snippet)

This snippet will help filter groq query by category or tags in a document. eg: post

By Surjith S M


groq filter in array reference

*[_type == "posts" && $keyword in categories[]->slug.current] {
  ...,
  categories[] -> {
          title,
          slug
  },
}

// Params
{
"keyword": "general"
}

groq filter in inline array

*[_type == "posts" && $keyword in tags[].label] {
  ...,
}

// Params
{
"keyword": "sanity"
}

This will help to filter a document using category tags or any other array. It can be an inline array or array reference.

The `in` keyword is used to find in an array. Learn more on the GROQ Cheatsheet

Contributor

Surjith S M

Web Designer & Front-end Developer. Figma, TailwindCSS & Next.js

India

Visit Surjith S M's profile