Join live โ€“ Get insights, tips, + Q&A from Sanity developers on our latest releases

How to query posts based on categories in

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',

Sep 29, 2022, 1:44 PM
Hey User, can possibly help: is this when using GROQ to query the data, or is it within the Sanity Studio itself?
Sep 29, 2022, 2:03 PM
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?
Sep 29, 2022, 2:05 PM
Everything seems to be correctly set up in the studio, it refs to the correct category.
Sep 29, 2022, 2:06 PM
Cool we actually use this ourselves in a query
Sep 29, 2022, 2:07 PM
Two seconds
Sep 29, 2022, 2:08 PM
Let me get it
Sep 29, 2022, 2:08 PM
*[_type == "post" && references(*[ title == "Front-end"]._id)]{...}
Sep 29, 2022, 2:12 PM
Something along those lines might help
Sep 29, 2022, 2:13 PM
thanks alot User, ill try em out!
Sep 29, 2022, 2:14 PM
Good luck buddy, I'm still looking at improving my GROQ knowledge but I would recommend checking these two places:
Sep 29, 2022, 2:16 PM
Side note: if you get stuck, feel free to ping me again ๐Ÿ‘
Sep 29, 2022, 2:17 PM
Hell yeah, thanks for your time!
Sep 29, 2022, 2:20 PM
it worked๐ŸŽ‰
Sep 29, 2022, 2:21 PM
Wicked ๐ŸŽ‰ glad to hear it, have a good one!
Sep 29, 2022, 2:24 PM
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
Sep 30, 2022, 12:20 PM
Totally doable
Sep 30, 2022, 12:23 PM
I've seen it somewhere in Sanity
Sep 30, 2022, 12:24 PM
Might be able to do it with
*[_type == "category"]
Or something along those lines
Sep 30, 2022, 12:25 PM
Use it as a separate query and then essentially match the categories together
Sep 30, 2022, 12:25 PM
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!
Sep 30, 2022, 12:26 PM
Just to confirm - you are expanding references e.g
*[_type == "category"]{..., reference->}

โ˜๏ธ this is a really simple version, but it's worth looking into
Sep 30, 2022, 12:30 PM
Also do you know about the egghead course? Because that's worth a look at too
Sep 30, 2022, 12:30 PM
It's legitimately really good for learning stuff (coming from somebody who buys tutorials then never watches them)
Sep 30, 2022, 12:31 PM
Thanks a lot man, gonna check it ๐Ÿ™‚ I did not expand it like that, I really need to read up on the groq ha.
Sep 30, 2022, 12:31 PM
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
Sep 30, 2022, 12:33 PM

Sanityโ€“ build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?