πŸ‘€ Our most exciting product launch yet πŸš€ Join us May 8th for Sanity Connect

Sanity query filter by category or tags (Groq Snippet)

By Surjith S M

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

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

Other schemas by author