Get all articles in a category for archive page

12 replies
Last updated: Nov 29, 2025
been searching through slack and trying to follow the Groq cheat sheet, but not finding what i need. can someone help?
i want to get all
articles
in a
category
. what's the best way to do so?
i'm trying to create a category archive page for each category
AI Update

Perfect! 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:

If category is an array of references

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 category is a single reference

If each article only has one category reference:

*[_type == "article" && category->slug.current == $categorySlug] {
  _id,
  title,
  slug,
  category-> {
    title,
    slug
  }
}

The key syntax explained

  • The in operator checks if a value exists in an array
  • categories[]-> follows the reference and gets the actual category documents
  • slug.current accesses the slug field from those referenced documents
  • The -> operator is how you dereference (follow) references in GROQ

For your category archive page

You'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!

Show original thread
12 replies

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.

Was this answer helpful?