Querying categories and posts in Sanity.io using GROQ

7 replies
Last updated: Jun 10, 2021
Hi! First project w/ Sanity and since it seemed easiest (because it was the example in the tutorial πŸ€·β€β™€οΈ) and I thought I could modify it later I chose Blog as a template.I'm doing a site where I'm going to display different categories of handicrafts - with name/title and an image (used Post to set up this).
I've added all the different categories in Category and connected each post to the correct category.
My question is, is it possible to query on category, and how do I do that?
AI Update

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:

  1. Filters posts where the category's slug is "woodworking"
  2. Returns the post's title and image
  3. 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 thread
7 replies
What exactly do you want to do? Do you want to get all categories and their posts? In GROQ that would be this query
*[ _type == "category"]{ ..., "posts": *[_type=="post" && references(^._id ) }
This gets every category, and for each category it gets every
post
that references that category. This is done using the
^
parent-operator ( https://www.sanity.io/docs/groq-operators#3715ad969fce ) and the
references
-function ( https://www.sanity.io/docs/groq-functions#5759ed6ff047 ).
I will have links to each category and when I click a specific link I want to query and see all posts for that category. For example this one:
Do note that this query will also include drafts unless you explicitly exclude them with
&& !(_id in path("drafts.*"))
inside the filter-component (
[ ]
). Path: https://www.sanity.io/docs/groq-functions#0ecd1b7eac78 In:
https://www.sanity.io/docs/groq-operators#c06332116311 filter:
https://www.sanity.io/docs/how-queries-work#e9a260f5e979
In that case the first query seems like it should match. It can be extended with
*[ _type == "category" && _id == $id]{ ..., "posts": *[_type=="post" && references(^._id ) }
and you pass in the
_id
of the category you want to find all posts for in order to only get the relevant data for that category.
A simpler version could be:

*[ _type=="post" && references($id)]
depending on if you need any information on the given category this could be extended to

*[ _type=="post" && references($id)]{..., category-> }
dereference:
https://www.sanity.io/docs/groq-operators#dae298fc7952
Thank you! Will try it out πŸ™ƒ
docs on parameters that I used: https://www.sanity.io/docs/groq-parameters
Good luck πŸ˜„

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.

Was this answer helpful?