Joint session with Vercel: How to build intelligent storefronts (May 15th)

GROQ join: Filter products by category name using reference

15 repliesLast updated: Nov 29, 2025

Hello! I'm new here and am learning Sanity for a job I just got. I've been messing with the default ecommerce dummy data and am having a lot of trouble doing a GROQ join. Basically I am trying to search for a product with a category name, something like this but I am sure I am quite off base what I've tried so far. Thanks for any help 🙂
*[ _type == "product" ]{
  ...,
  "categories": *[ _type == "category" && ^._id in product[]._ref && slug.current == "swedish"],
}
AI Update

Welcome to Sanity! Great question - GROQ joins can be tricky at first, but once you get the hang of references, they're quite powerful.

The issue with your query is that you're trying to do the join "backwards." Instead of nesting another query inside, you want to dereference the category references and then filter the products based on those dereferenced values.

Here's the corrected approach:

*[_type == "product" && "swedish" in categories[]->slug.current] {
  ...,
  "categories": categories[]->{
    _id,
    title,
    slug
  }
}

Let me break down what's happening:

If you want to search by category name instead of slug, you can use:

*[_type == "product" && "Swedish" in categories[]->title]

Alternative using references(): If you already know the category's _id, you can use the references() function:

*[_type == "product" && references(*[_type == "category" && slug.current == "swedish"]._id)]

The Sanity GROQ recipe on filtering by category has great examples of this pattern. The key thing to remember is that the in keyword is used to find matches in an array, and the -> operator dereferences your array of category references so you can access their properties.

You can also check out the GROQ Query Cheat Sheet for more examples of working with references and arrays!

Show original thread
15 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