Querying categories and posts in Sanity.io using GROQ
Great question! Yes, you can absolutely query posts by category in GROQ. Since you're using the blog template where posts have a category reference, you have a couple of options:
Filtering Posts by Category
The most straightforward approach is to filter posts where the category reference matches a specific category ID:
*[_type == "post" && category._ref == "your-category-id"]Or if you want to match by the category's slug or title, you can use the reference access operator (->) to dereference the category and check its properties:
*[_type == "post" && category->slug.current == "woodworking"] {
title,
mainImage,
category->{
title,
slug
}
}This query:
- Filters posts where the category's slug is "woodworking"
- Returns the post's title and image
- Resolves the category reference to include the full category details
Getting All Posts Grouped by Category
If you want to get all your handicraft categories with their associated posts, you can do a reverse reference lookup:
*[_type == "category"] {
title,
"posts": *[_type == "post" && references(^._id)] {
title,
mainImage
}
}This gives you each category with an array of all posts that reference it - perfect for displaying all your handicraft categories with their associated items!
In Your Frontend Code
If you're using Next.js or another framework, you'd typically query like this:
const query = `*[_type == "post" && category->slug.current == $categorySlug] {
title,
slug,
mainImage,
category->
}`
const posts = await client.fetch(query, { categorySlug: 'woodworking' })The -> operator is the key here - it "dereferences" the reference, fetching the actual category document instead of just the reference ID. You can use it to both filter by category properties and to include the full category data in your results.
Show original thread7 replies
Sanity β Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.