GROQ: How to fetch all items except the first one using slice?

2 replies
Last updated: Oct 6, 2023
Hello, I am wondering if it's possible in GROQ to fetch all items in the collections without include the first entry? Is it possible to slice that way? For example, if I have a series of post that I ordered by their publish date, how would I get all of them besides the first one?
AI Update

Yes, absolutely! GROQ's slice component is perfect for this. You can use array slicing to skip the first entry and get all the rest.

Here's how to do it:

*[_type == "post"] | order(publishDate desc) [1..-1]

This query:

  1. Fetches all posts
  2. Orders them by publish date (descending)
  3. Uses [1..-1] to slice from index 1 (the second item) to the end

The slice syntax in GROQ works like this:

  • [1..-1] - starts at index 1 (second item) and goes to the last item
  • [1...] - also works, starting at index 1 and going to the end (using ... excludes the right-hand boundary)
  • [0] - would give you just the first item
  • [1..5] - would give you items at indices 1 through 5

The key thing to remember is that GROQ uses zero-based indexing, so:

  • 0 is the first item
  • 1 is the second item
  • -1 refers to the last item

So if you want everything except the first entry, [1..-1] or [1...] is what you need. Just make sure to apply your ordering before the slice, otherwise you'll be skipping the wrong item!

You can read more about pipeline components and slicing syntax in the GROQ documentation.

Show original thread
2 replies
If you want to post your current query, that may help us be more specific, but generally, you can slice an array and use
[1..-1]
notation to return all but the first (zeroth) index.
Excellent, thank you very much, that worked great.

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?