Query posts by category reference in Sanity schema

25 replies
Last updated: Sep 30, 2022
So i have my postQuery, and I want to get a post based on a category i have attached to it, im able to get just the category, but not when I go via post -> categories, any ideas?
This is in my post.js

    name: 'categories',
      title: 'Categories',
      type: 'array',
      of: [{ type: 'reference', to: { type: 'category' } }],
    },
This is in my category.js

  name: 'category',
  title: 'Category',
  type: 'document',
  fields: [
    {
      name: 'title',
      title: 'Title',
      type: 'string',
    },
    {
      name: 'description',
      title: 'Description',
      type: 'text',
    },
  ],

AI Update

Based on your schema, you have posts with an array of category references. The key to querying posts filtered by category is understanding how to work with reference arrays in GROQ.

Here's how to query posts by category:

Filter posts that have a specific category:

*[_type == "post" && references("category-id-here")] {
  title,
  categories[]->
}

The references() function checks if any reference in your categories array points to the specified category ID.

Get the full category details:

To dereference the categories array and get the actual category data (not just the references), use the -> operator:

*[_type == "post"] {
  title,
  categories[]->{
    title,
    description
  }
}

The categories[]-> syntax means "for each item in the categories array, dereference it and return the full document."

Filter by category title/slug:

If you want to filter by a category's title instead of its ID:

*[_type == "post" && "Technology" in categories[]->title] {
  title,
  categories[]->
}

Common mistake to avoid:

Don't try categories-> without the [] - that won't work because categories is an array, not a single reference. The [] tells GROQ to iterate through the array items.

The reference access operator (->) performs a subquery internally to fetch the referenced document. When you use categories[]->, it resolves each reference in the array and replaces it with the actual category document data.

If you're still having issues, the problem is likely in how you're structuring the query - make sure you're using categories[]-> (with the brackets) rather than categories-> when working with your array of references.

Show original thread
25 replies
Hey User, can possibly help: is this when using GROQ to query the data, or is it within the Sanity Studio itself?
Thanks User! This is in the front-end, i have some posts with the category ‘Front-end’, and would like to only show them. Guessing i have to make a combined query to get this result?
Everything seems to be correctly set up in the studio, it refs to the correct category.
Cool we actually use this ourselves in a query
Two seconds
Let me get it
*[_type == "post" && references(*[ title == "Front-end"]._id)]{...}
Something along those lines might help
thanks alot User, ill try em out!
Good luck buddy, I'm still looking at improving my GROQ knowledge but I would recommend checking these two places:
https://www.sanity.io/docs/query-cheat-sheet
https://www.youtube.com/watch?v=vqfMVEYDm0U
Side note: if you get stuck, feel free to ping me again 👍
Hell yeah, thanks for your time!
it worked🎉
Wicked 🎉 glad to hear it, have a good one!
Hey User, do you think its possible to get all categories with that query, and just filter the one I need with js? So i dont have to create multiple querys
Totally doable
I've seen it somewhere in Sanity
Might be able to do it with
*[_type == "category"]
Or something along those lines

https://www.sanity.io/docs/query-cheat-sheet
Use it as a separate query and then essentially match the categories together
Yeah that was the way I tried to do it before I asked the question here, problem then was that I couldnt get any title out of the category that was attached to the post, ill just have to do some more digging, thanks!
Just to confirm - you are expanding references e.g
*[_type == "category"]{..., reference->}

☝️ this is a really simple version, but it's worth looking into https://www.sanity.io/docs/how-queries-work#8ca3cefc3a31
Also do you know about the egghead course? Because that's worth a look at too https://egghead.io/courses/introduction-to-groq-query-language-6e9c6fc0
It's legitimately really good for learning stuff (coming from somebody who buys tutorials then never watches them)
Thanks a lot man, gonna check it 🙂 I did not expand it like that, I really need to read up on the groq ha.
Oh yeah, go for the egghead one and use the cheat sheet for backup! 👍 Give me a shout again if you get hit on any hard roadblocks

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?