How to query posts based on categories in Sanity.io
25 replies
Last updated: Sep 30, 2022
N
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
This is in my category.js
This is in my post.js
name: 'categories', title: 'Categories', type: 'array', of: [{ type: 'reference', to: { type: 'category' } }], },
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
N
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
N
Everything seems to be correctly set up in the studio, it refs to the correct category.
Sep 29, 2022, 2:06 PM
N
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:
https://www.sanity.io/docs/query-cheat-sheet
https://www.youtube.com/watch?v=vqfMVEYDm0U
https://www.sanity.io/docs/query-cheat-sheet
https://www.youtube.com/watch?v=vqfMVEYDm0U
Sep 29, 2022, 2:16 PM
N
Hell yeah, thanks for your time!
Sep 29, 2022, 2:20 PM
N
it workedπ
Sep 29, 2022, 2:21 PM
N
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
Might be able to do it with
Or something along those lines
https://www.sanity.io/docs/query-cheat-sheet
*[_type == "category"]
https://www.sanity.io/docs/query-cheat-sheet
Sep 30, 2022, 12:25 PM
N
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
βοΈ this is a really simple version, but it's worth looking into https://www.sanity.io/docs/how-queries-work#8ca3cefc3a31
*[_type == "category"]{..., reference->}
βοΈ this is a really simple version, but it's worth looking into https://www.sanity.io/docs/how-queries-work#8ca3cefc3a31
Sep 30, 2022, 12:30 PM
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
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
N
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.