How to paginate GROQ query with filtered author reference in blog posts
I can help you with filtering and paginating blog posts by author reference! This is a common challenge in GROQ because you need to filter by a referenced document's field before expanding the reference.
The key is to use the dereferencing operator (->) directly in your filter, not just in the projection. Here's how to structure your query:
*[_type == "post" && author->slug.current == "author-slug-here"] | order(publishedAt desc) [0...10] {
_id,
title,
publishedAt,
author-> {
name,
slug
}
}Or if you want to filter by author name:
*[_type == "post" && author->name == "John Doe"] | order(publishedAt desc) [0...10] {
_id,
title,
publishedAt,
author-> {
name,
slug
}
}Key points:
Filter with dereference: Use
author->slug.currentorauthor->namedirectly in your filter condition. This tells GROQ to follow the reference during filtering.Pagination: The
[0...10]slice gives you the first 10 results. For subsequent pages, use[10...20],[20...30], etc.Expand in projection: You can still expand the full author reference in your projection using
author-> { ... }to get all the author fields you need.
If you need more efficient pagination for large datasets, consider using a cursor-based approach, which uses the last document's value as a starting point rather than array slicing:
*[_type == "post" && author->slug.current == "author-slug-here" && publishedAt < $lastPublishedAt]
| order(publishedAt desc) [0...10]This approach performs better at scale because it doesn't need to skip over all previous results—you just pass in the publishedAt value from the last item of the previous page as the $lastPublishedAt parameter.
Show original thread8 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.