👋 Next.js Conf 2024: Come build, party, run, and connect with us! See all events

Help with GROQ query to select blog posts with specific category in Sanity

18 replies
Last updated: Aug 11, 2021
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?
Aug 7, 2021, 6:06 PM
Thanks for your reply.
Aug 7, 2021, 7:14 PM
Its doesn't work 😔
Aug 7, 2021, 7:15 PM
Looks like it didn’t copy over quite right. You’ll want
slug.current]
after the
->
.
Aug 7, 2021, 7:17 PM
I'm sorry.
Aug 7, 2021, 7:20 PM
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
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
'content-marketing' in categories[]->slug.current
part is doing. It’s saying “traverse through each of the categories in the
categories
array (that’s in
post
documents), get the actual category documents (using the
->
dereference operator—otherwise, you’ll only have the reference properties), get
slug.current
for 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
post
document.”
All of that said, it assumes that your schema is set up with
post
documents that have an array of references named
categories
that references your category documents, and that they have a slug on them.
Aug 7, 2021, 7:25 PM
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
'content-marketing' in categories[]->slug.current
part is doing. It’s saying “traverse through each of the categories (in
post
documents), get the actual category documents (using the
->
dereference operator—otherwise, you’ll only have the reference properties), get
slug.current
for 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
post
document.
Aug 7, 2021, 7:25 PM
Thanks a lot.
Aug 7, 2021, 7:27 PM
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
I wrote it
*[_type == 'post' && 'content-marketing' in categories[]->slug.current]
Aug 7, 2021, 7:31 PM
Can i share my category schema with you?
Aug 7, 2021, 7:32 PM
This is my category schema
Aug 7, 2021, 7:34 PM
This is my post schema
Aug 7, 2021, 7:35 PM
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 (
[]
) and will change
in
to
==
. Can you try this?

*[_type == 'post' && categories->slug.current == 'content-marketing']
Aug 7, 2021, 7:47 PM
Thanks a lot ❤️. Its work. Many many thanks
Aug 7, 2021, 7:49 PM
You’re welcome. Have fun building!
Aug 7, 2021, 7:49 PM
Hello
user A
sir! I need some help, if you are free.
Aug 11, 2021, 2:48 PM
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.

Was this answer helpful?