🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Understanding how to query categories in Sanity schema using Groq

13 replies
Last updated: Aug 8, 2020
could you explain your schema a little bit it seems like categories is self referencing array but there is no value like Category name etc...?
Aug 8, 2020, 10:33 AM
Thank you for your message! My Categories schema looks like this

export default {
  name: "categories",
  title: "Categories",
  type: "document",
  fields: [
      name: "title",
      title: "Title",
      type: "string",
      name: "slug",
      title: "Slug",
      type: "slug",
      options: {
        source: "title",
        maxLength: 96,
      name: "description",
      title: "Description",
      type: "text",
      name: "featuredImage",
      title: "Featured image",
      type: "image",
      options: {
        hotspot: true,
There is no name, but there is a
field. So I'm trying to understand how I can make groq query to look into those fields. maybe something like in this query https://sanity-io-land.slack.com/archives/C011CAT70DD/p1586980014009300?thread_ts=1586979557.003700&cid=C011CAT70DD but this didn't work for me when I did

*[ _type== 'post' && categories->title == 'category name']
Aug 8, 2020, 10:47 AM
also tried this
*[_type == 'posts' && categories.title match 'category name' ]
I think I'm not getting the syntax
Aug 8, 2020, 10:50 AM
Because Categories would be an array I think you need categories [].title *[_type == 'posts' && 'category name' in categories []. title]
Aug 8, 2020, 11:16 AM
yeah, it does make sense. however, I still get
error. I'm making requests with axios. simple
works, get all the posts, but when I try to be more specific, I get 400. can it be that axios tampers with request somehow, do you know?
Aug 8, 2020, 11:21 AM
If you have vision deployed on your studio probably a good idea to test query in vision then investigate further, but No I have never used axios for sanity calls.
Aug 8, 2020, 11:24 AM
okay, I'll go try with vision, thank you for your help!
Aug 8, 2020, 11:25 AM
*[_type == 'posts' && categories[]->title match 'category name' ]

Try this one? You have to traverse the array of references
and then join the category document
and pick the
Aug 8, 2020, 11:29 AM
YASS! it worked, thank you. Also this worked. What's the difference between yours and this one?
*[ _type=='posts' && 'category name' in categories[]->title]
Aug 8, 2020, 11:36 AM
also follow up question, how do you include multiple categories names? do I have to
&& 'category name' in categories[]->title
every time or is there a shorter version?
Aug 8, 2020, 11:38 AM
Match can be fuzzy if you e.g add a
wildcard to the string. The
is only true on exact match
Aug 8, 2020, 11:38 AM
ah, makes sense, thank you! I'm gonna write a blog post to help others out as well! 👍
Aug 8, 2020, 11:39 AM
Not 100% sure about array comparisons tbh. And on my iPhone, so a bit hard to experiment:)
Aug 8, 2020, 11:45 AM
No worries, then I'll play around with it Was just asking in case there is a specific 'documented' way of doing things. Thanks a lot!
Aug 8, 2020, 11:55 AM

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?