GROQ: Fetch documents by category excluding latest 8 documents

17 replies
Last updated: Nov 29, 2025
Hello,
I want to fetch some documents that contains "dance" as category. But the documents should not be one of the latest 8 documents. How can I do this in GROQ?
AI Update

You can achieve this by combining filtering, ordering, and slicing in GROQ. Here's how to fetch documents with "dance" as a category while excluding the latest 8 documents:

*[_type == "yourDocumentType" && category == "dance"] 
  | order(_createdAt desc) [8..]

Let me break this down:

  1. *[_type == "yourDocumentType" && category == "dance"] - Filters documents to only those with "dance" as the category
  2. | order(_createdAt desc) - Sorts all matching documents by creation date, newest first (you could also use _updatedAt or any other date field)
  3. [8..] - Slices the array starting from index 8 to the end, effectively skipping the first 8 (latest) documents

The key here is the slice component syntax. Using [8..] means "start at index 8 and include everything after that." Since arrays are zero-indexed, this skips indices 0-7 (the 8 most recent documents).

If you want to also limit the total number of results, you can extend the slice:

*[_type == "yourDocumentType" && category == "dance"] 
  | order(_createdAt desc) [8..17]

This would give you documents 9-18 (skipping the latest 8, then returning the next 10).

Important note: Make sure to replace _createdAt with whatever date field makes sense for determining "latest" in your use case - it could be a custom publishedAt field or _updatedAt depending on your content model.

Show original thread
17 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?