Surjith S M
Web Designer & Front-end Developer. Figma, TailwindCSS & Next.js
This snippet will help filter groq query by category or tags in a document. eg: post
*[_type == "posts" && $keyword in categories[]->slug.current] {
...,
categories[] -> {
title,
slug
},
}
// Params
{
"keyword": "general"
}
*[_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
Web Designer & Front-end Developer. Figma, TailwindCSS & Next.js
Suppose you have an Array Reference to Category and you want to get all posts with one of the categories listed in the current post. This is usually when you need to get related items.
Go to Get related items of a post in sanity by comparing category array reference with another arrayA short snippet to filter category, tags or other document based on total referenced count.
Go to Sanity GROQ Filter category by highest reference count in other documentsSnippet for `internalLInk` Reference in GROQ and rendering it in React/Next.js frontend using Portabletext.
Go to Sanity internalLink render in React with Portable TextSanity preview component cannot access the parent/document if its inside an array or object. Using Custom Preview component, we can solve the issue.
Go to Sanity Custom Preview Component to get Parent Document