
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storePerfect! I found exactly what you need. When you want to get all articles in a category for creating category archive pages, you'll use the in operator in GROQ to filter by reference fields.
Here are the two main patterns depending on how your schema is set up:
This is the most common setup where your articles have a categories field that references category documents:
*[_type == "article" && $categorySlug in categories[]->slug.current] {
_id,
title,
slug,
categories[]-> {
title,
slug
}
}You'd pass in the category slug as a parameter:
{
"categorySlug": "technology"
}If each article only has one category reference:
*[_type == "article" && category->slug.current == $categorySlug] {
_id,
title,
slug,
category-> {
title,
slug
}
}in operator checks if a value exists in an arraycategories[]-> follows the reference and gets the actual category documentsslug.current accesses the slug field from those referenced documents-> operator is how you dereference (follow) references in GROQYou'll likely want to order and paginate results too:
*[_type == "article" && $categorySlug in categories[]->slug.current] | order(publishedAt desc) [0...12] {
_id,
title,
slug,
publishedAt,
excerpt,
mainImage,
categories[]-> {
title,
slug
}
}This gets the first 12 articles in a category, ordered by publish date. You can adjust the slice [0...12] for pagination.
The Sanity recipe on filtering by category has more examples if you need to handle inline arrays or other variations!
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store