👋 Next.js Conf 2024: Come build, party, run, and connect with us! See all events

How to fetch documents in GROQ that match a category but exclude the latest 8 documents.

17 replies
Last updated: Sep 27, 2021
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?
Sep 27, 2021, 3:06 AM
*[category match 'dance'] | order(date asc)[0...-8]
The filter matches
dance
, then those documents are sorted by date in ascending order (oldest first), and finally, all but the last eight documents are returned.
Alternatively, you could do:


*[category match 'dance'] | order(date desc)[8..-1]
That sorts in descending order and returns from the 9th document (index 8 in a zero-index count) to the last one. Notice this uses two dots instead of three.
Sep 27, 2021, 3:20 AM
the first 8 will have dance music and many other categories
Sep 27, 2021, 3:21 AM
*[category match 'dance'] | order(date asc)[0...-8]
The filter matches
dance
, then those documents are sorted by date in ascending order (oldest first), and finally, all but the last eight documents are returned.
Alternatively, you could do:


*[category match 'dance'] | order(date desc)[8..-1]
That sorts in descending order and returns from the 9th document (index 8 in a zero-index count) to the last one. Notice this uses two dots instead of three.
Sep 27, 2021, 3:20 AM
If I'm matching dance and saying 8..-1, Sanity will avoid the first 8 documents with "dance" category right?
Sep 27, 2021, 3:23 AM
the first 8 will have dance music and many other categories
Maybe you’re after this then?

*[] | order(date asc)[0...-8][category match 'dance']

If I’m matching dance and saying 8..-1, Sanity will avoid the first 8 documents with “dance” category right?
In the first examples I posted, yes. If you just want the
dance
documents from all documents that aren’t in the last eight (of all documents), you could try the query above.
Sep 27, 2021, 3:24 AM
I'll show the query
The query I use to fetch first 8

*[_type == "post"&& featured != true] | order(publishedAt desc) {_id}[0...8]
Sep 27, 2021, 3:25 AM
The query I use to get a specific category items

*[_type == 'post' && *[_type == "category" && featured != true && slug.current == "${cat}"][0]._id in categories[]._ref] | order(publishedAt desc) {_id}[0...${count}]
Sep 27, 2021, 3:26 AM
Actually I'm trying to get blogs in specific categories, but they should not be included in the latest 8 documents
Sep 27, 2021, 3:27 AM
You want the first half of your category query to be the same as your first 8 query, and then do something similar to what Geoff suggested:

*[_type == "post" && featured != true] | order(publishedAt desc) [8..-1][references("catId")] { _id }
An example:
https://groq.dev/KCY0kE1tA2rNLI7cDcdECC
Sep 27, 2021, 4:58 AM
You want the first half of your category query to be the same as your first 8 query, and then do something similar to what Geoff suggested:

*[_type == "post" && featured != true] | order(publishedAt desc) [8..-1][references("catId")] { _id }
An example:
https://groq.dev/KCY0kE1tA2rNLI7cDcdECC
Sep 27, 2021, 4:58 AM
Hi
user G
, I my requirement is that the first 8 documents should not appear in any other query
Sep 27, 2021, 5:38 AM
yes, if you do [8..-1] then they should not? You can modify the query I share in the groq playground to be similar to your first query:
*[_type == "post" && featured != true] | order(publishedAt desc) [0...8]
& you’ll see that they don’t overlap
Sep 27, 2021, 5:38 AM
is there any way to reference slug instead of catId?
Sep 27, 2021, 5:41 AM
is there any way to reference slug instead of catId?
Sep 27, 2021, 5:41 AM
the post references one or more categories as an array
Sep 27, 2021, 5:42 AM
Yeah I think so, you’d need to query for the
_id
from inside the
references()
function:
*[_type == "post"] | order(order desc) [2..-1] [references(*[_type == "cat" && title == 'Cat 1'][0]._id)]
Sep 27, 2021, 5:45 AM
We resolved this issue in DM — the query above worked!
Sep 27, 2021, 3:08 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?