GROQ: How to fetch all items except the first one using slice?
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:
- Fetches all posts
- Orders them by publish date (descending)
- 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:
0is the first item1is the second item-1refers 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 thread2 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.