This Friday: Hear from Sanity + Vercel experts on AI and better personalization in e-commerce

How to filter articles by category slug when categories is an array of references?

7 repliesLast updated: Nov 29, 2025

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 ?
AI Update

I can help you fix this GROQ query issue! The problem is with how you're trying to use the references() function. The syntax you have isn't valid GROQ.

The references() function is actually much simpler than what you're attempting. It takes a document ID and checks if any reference field in the document points to it. You don't specify field paths or slugs - it just checks all reference fields automatically.

Here's the corrected query:

const query = groq`*[_type == "article" && references($categoryId)] {
    ...,
    categories[]-> {
      title,
      slug
    }  
}`

The key changes:

In your code, you'll need to:

const categoryDoc = await client.fetch(
  `*[_type == "category" && slug.current == $slug][0]`,
  { slug }
)
const articles = await client.fetch(query, { categoryId: categoryDoc._id })

Alternative: If you must filter by slug directly, you can use this approach instead:

*[_type == "article" && $slug in categories[]->slug.current] {
    ...,
    categories[]-> {
      title,
      slug
    }  
}

This dereferences all categories in the array and checks if your slug exists in any of them. However, this can be less performant than using references() with the document ID, because it has to dereference every category reference to check the slugs.

The references() function is specifically designed for checking if a document references another document by ID, which is why it's the recommended and most efficient approach when working with reference fields.

Show original thread
7 replies

Was this answer helpful?

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Related contributions