Understanding how to query categories in Sanity schema using Groq
13 replies
Last updated: Aug 8, 2020
Q
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
W
Thank you for your message! My Categories schema looks like this
There is no name, but there is a
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, }, }, ], };
titleand
slugfield. 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
W
also tried this
I think I'm not getting the syntax
*[_type == 'posts' && categories.title match 'category name' ]
Aug 8, 2020, 10:50 AM
Q
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
W
yeah, it does make sense. however, I still get
400error. I'm making requests with axios. simple
*[_type=='posts']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
Q
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
W
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
categories[]and then join the category document
->and pick the
titlefield
Aug 8, 2020, 11:29 AM
W
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
W
also follow up question, how do you include multiple categories names? do I have to every time or is there a shorter version?
&& 'category name' in categories[]->title
Aug 8, 2020, 11:38 AM
Match can be fuzzy if you e.g add a
*wildcard to the string. The
inis only true on exact match
Aug 8, 2020, 11:38 AM
W
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
W
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.