GROQ query to filter blog posts by category slug

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?
AI Update

Hey there! I can help you with that. To filter blog posts by a category with a specific slug, you need to use the reference access operator (->) in your GROQ query to dereference the category reference and access its slug field.

Here's the query you need:

*[_type == "post" && category->slug.current == "content-marketing"]

Let me break this down:

  • *[_type == "post"...] - Filters all documents of type "post" (adjust "post" to match your blog post document type)
  • category-> - The reference access operator dereferences your category reference to access the actual category document
  • slug.current == "content-marketing" - Accesses the slug field (typically stored as slug.current in Sanity) and checks if it equals "content-marketing"

If you want to include the full category data in your results, you can add a projection:

*[_type == "post" && category->slug.current == "content-marketing"] {
  _id,
  title,
  category->,
  // other fields you need
}

Common issues to watch out for:

  1. Make sure your document type name matches (might be "blogPost" or "article" instead of "post")
  2. The slug field is usually slug.current, not just slug
  3. If the category field is an array of references, use "content-marketing" in category[]->slug.current instead

You can test this query in Sanity Vision (the Vision plugin in your Studio) to make sure it returns the right results before using it in your code.

Let me know if you're still having issues and I can help you troubleshoot further!

Show original thread
18 replies
Thanks for your reply.
Its doesn't work 😔
Looks like it didn’t copy over quite right. You’ll want
slug.current]
after the
->
.
I'm sorry.
But I want to use this query in sanity vision, so I replace slug.current with content-marketing. Am I wrong?
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.
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.
Thanks a lot.
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.
I wrote it
*[_type == 'post' && 'content-marketing' in categories[]->slug.current]
Can i share my category schema with you?
This is my category schema
This is my post schema
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']
Thanks a lot ❤️. Its work. Many many thanks
You’re welcome. Have fun building!
Hello
user A
sir! I need some help, if you are free.
Hello
user A
sir! I need some help, if you are free.

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?