👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Problem with croq query on category page, query not returning any results. Seeking help and suggestions.

7 replies
Last updated: Jul 31, 2023
Hello everyone, I have a problem with croq query
  const query = groq`*[_type == "article" && references("category", "categories[]->slug", "${slug}")] {
    ...,
    categories[]-> {
      title,
      slug
    }  
  }
I am on the category page and I try to get all the articles that contain my category slug "${slug}" in the ctegories list. The thing is that the category list "array" is a list of references from Category. Now my query brings nothing. Did you already face it ?
Jul 31, 2023, 1:10 PM
I would recommend trying out the GROQ query in Vision Plugin first and ensure that it is working.Also, from what I understand variables should be passed in as
params ?
Jul 31, 2023, 1:17 PM
It's just that I don't know if it's possible to do a query to get "article" that have a list "categories" of reference "category" with a slug that match my slug.
I found this to do the match

references("category", "categories[]->slug", $slug)
But it doesn't work in Vision Plugin
Jul 31, 2023, 2:54 PM
I don't know find anything else
Jul 31, 2023, 2:54 PM
Something like this maybe?
*[_type == "article" && categories[]->slug match $slug]{
  title,
  "slug": categories[]->slug,
}
Jul 31, 2023, 2:59 PM
Wow merci
Jul 31, 2023, 3:35 PM
It works with your solution
Jul 31, 2023, 3:36 PM
thanks
Jul 31, 2023, 3:36 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?