Help with GROQ query to select blog posts with specific category in Sanity
18 replies
Last updated: Aug 11, 2021
F
Hello everyone! How are you?I'm still stuck with the same problem.
actually I want to make a GROQ query that selects all my blog post with a category with the slug "content-marketing".
I search this problem in google and find a solution at StackOverflow, but it doesn't work for me.
How can i do that?
actually I want to make a GROQ query that selects all my blog post with a category with the slug "content-marketing".
I search this problem in google and find a solution at StackOverflow, but it doesn't work for me.
How can i do that?
Aug 7, 2021, 6:06 PM
F
Thanks for your reply.
Aug 7, 2021, 7:14 PM
F
Its doesn't work 😔
Aug 7, 2021, 7:15 PM
G
Looks like it didn’t copy over quite right. You’ll want
slug.current]after the
->.
Aug 7, 2021, 7:17 PM
F
I'm sorry.
Aug 7, 2021, 7:20 PM
F
But I want to use this query in sanity vision, so I replace slug.current with content-marketing. Am I wrong?
Aug 7, 2021, 7:21 PM
G
Don’t be sorry! It can be confusing stuff to get used to.
It’s actually the query itself that will do that work for you. That’s what the
part is doing. It’s saying “traverse through each of the categories in the
All of that said, it assumes that your schema is set up with
It’s actually the query itself that will do that work for you. That’s what the
'content-marketing' in categories[]->slug.current
categoriesarray (that’s in
postdocuments), get the actual category documents (using the
->dereference operator—otherwise, you’ll only have the reference properties), get
slug.currentfor each of those documents and put them into an array, and finally, see if any of them are
content-marketing. Where they are, return that
postdocument.”
All of that said, it assumes that your schema is set up with
postdocuments that have an array of references named
categoriesthat references your category documents, and that they have a slug on them.
Aug 7, 2021, 7:25 PM
G
Don’t be sorry! It can be confusing stuff to get used to.
It’s actually the query itself that will do that work for you. That’s what the
part is doing. It’s saying “traverse through each of the categories (in
It’s actually the query itself that will do that work for you. That’s what the
'content-marketing' in categories[]->slug.current
postdocuments), get the actual category documents (using the
->dereference operator—otherwise, you’ll only have the reference properties), get
slug.currentfor each of those documents and put them into an array, and finally, see if any of them are
content-marketing. Where they are, return that
postdocument.
Aug 7, 2021, 7:25 PM
F
Thanks a lot.
Aug 7, 2021, 7:27 PM
G
No problem. If it still doesn’t work, you can post what you get when you run
*[_type == 'post']and we’ll figure it out from there.
Aug 7, 2021, 7:28 PM
F
I wrote it*[_type == 'post' && 'content-marketing' in categories[]->slug.current]
Aug 7, 2021, 7:31 PM
F
Can i share my category schema with you?
Aug 7, 2021, 7:32 PM
F
This is my category schema
Aug 7, 2021, 7:34 PM
F
This is my post schema
Aug 7, 2021, 7:35 PM
G
Ahh, perfect! So this is a direct reference rather than an array of references (meaning you can only ever select one). My fault for assuming otherwise.
You’ll want to change your query so that rather than looking for a string in an array, you’re testing equality. You’ll remove the array traversal of categories (
You’ll want to change your query so that rather than looking for a string in an array, you’re testing equality. You’ll remove the array traversal of categories (
[]) and will change
into
==. Can you try this?
*[_type == 'post' && categories->slug.current == 'content-marketing']
Aug 7, 2021, 7:47 PM
F
Thanks a lot ❤️. Its work. Many many thanks
Aug 7, 2021, 7:49 PM
G
You’re welcome. Have fun building!
Aug 7, 2021, 7:49 PM
F
Hello
user A
sir! I need some help, if you are free.Aug 11, 2021, 2:48 PM
F
Hello
user A
sir! I need some help, if you are free.Aug 11, 2021, 2:48 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.