🎤 Builder Talk: The Story Behind Lady Gaga’s Digital Experience – Register now

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
title
and
slug
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
400
error. 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
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
categories[]
and then join the category document
->
and pick the
title
field
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
in
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?

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
Hey all - what’s the quickest way to batch delete all documents of a certain type? It looks like the `sanity` cli tool allows...Jan 29, 2021
Rendering nested block contentJan 18, 2021
In the tutorial, there is samples of what the API returns as JSON. Is there a way to view these ?Jan 11, 2021
Warning: Data for Page Exceeds the Threshold of 128 kBAug 17, 2022
List Arrays - Is it Possible to Fill a List Array Within a Schema Asynchronously?Mar 16, 2021
Custom Document Views - Is it Even Possible to Use the Structure Builder?Apr 21, 2022
How to get the number of nested/children components in a GROQ query.Jan 19, 2024
TypeError: Cannot Read Properties of Null (Reading 'jsonType')Sep 28, 2022
Error in custom validation rule for schema in Slack threadAug 18, 2023
Issue with fetching data from an array of references and non-references in a groq query.Dec 14, 2023

Related contributions

Turbo Start Sanity
- Template

The battle-tested Sanity template that powers Roboto Studio's websites

Go to Turbo Start Sanity

Schema UI - Next.js Sanity Starter
- Template

A Next.js starter template with Next.js 15, Tailwind CSS, shadcn/ui, and Sanity CMS with Live Editing. Get production-ready React components with matching Sanity schemas and queries. Build dynamic pages faster while keeping full control over customization.

Serge Ovcharenko
Go to Schema UI - Next.js Sanity Starter

The Swaddle
- Made with Sanity

A new brand identity to represent a more mature company, to signify The Swaddle’s evolution from publisher to production house, combined with an easier to navigate platform that can surface multiple content types - drawing readers through The Swaddle’s content offering.

Nightjar
Go to The Swaddle