Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

How to retrieve posts from a specific category using Sanity.io.

6 replies
Last updated: Aug 15, 2023
*[_type == "category"]{
    "post": *[_type == "post" && category._id == ^.categories._ref]
I am trying to get all post that are in a category. The code above is retrieving all categories as expected. and from the post key I am getting date. The problem is that the post key is showing all post regardless if they are in the category. I only want it to show the post in that category. Is there a way to do this. I am using the default blog schema
Aug 15, 2023, 3:54 PM
Maybe you can try using
"post": *[_type == "post" && references(^._id))
You can find an example
here .
Aug 15, 2023, 4:09 PM
That worked great. I feel I have a lot to learn. Is there a query generator
Aug 15, 2023, 4:15 PM
Glad that the solution worked for you :) I am not aware of any query generators unfortunately.For learning purposes, you can also take a look at the
GROQ arcade in addition to the Vision plugin and GROQ in 45 min .
Aug 15, 2023, 4:28 PM
I was able to get the same result using openai
Aug 15, 2023, 6:21 PM
using sanity groq how do you retrieve all posts from a certain category
followed up by
do this for every category
Aug 15, 2023, 6:21 PM
it returned that exact query Urvashi shared.
Aug 15, 2023, 6:22 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?