How to fetch blogPosts filtered on category in Sanity.io.

3 replies
Last updated: Feb 6, 2024
How do I fetch all
blogPosts
filtered on
category
? (
categories
is an array of references to
blogPostCategory
)
I have tried:

"blogPosts": *[_type == "blogPost" && categories[]->slug.current == "news"] { ... }
and:

"blogPosts": *[_type == "blogPost" && (categories[]->slug.current)[@ in ["news"]]] { ... }

🙏
Feb 6, 2024, 2:03 PM
"blogPosts": *[_type == "blogPost" && "news" in categories[]->slug.current] { ... }
does that work?
Feb 6, 2024, 2:16 PM
Yes!! Oh man was it just a js syntax misstake 😅 Thanks alot
Feb 6, 2024, 2:19 PM
Feb 6, 2024, 2:20 PM

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?