How to fetch documents in GROQ that match a category but exclude the latest 8 documents.
17 replies
Last updated: Sep 27, 2021
S
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?
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]
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]
Sep 27, 2021, 3:20 AM
S
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]
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]
Sep 27, 2021, 3:20 AM
S
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 categoriesMaybe 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
dancedocuments 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
S
I'll show the query
The query I use to fetch first 8
The query I use to fetch first 8
*[_type == "post"&& featured != true] | order(publishedAt desc) {_id}[0...8]
Sep 27, 2021, 3:25 AM
S
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
S
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
D
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:
An example:
https://groq.dev/KCY0kE1tA2rNLI7cDcdECC
*[_type == "post" && featured != true] | order(publishedAt desc) [8..-1][references("catId")] { _id }
https://groq.dev/KCY0kE1tA2rNLI7cDcdECC
Sep 27, 2021, 4:58 AM
D
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:
An example:
https://groq.dev/KCY0kE1tA2rNLI7cDcdECC
*[_type == "post" && featured != true] | order(publishedAt desc) [8..-1][references("catId")] { _id }
https://groq.dev/KCY0kE1tA2rNLI7cDcdECC
Sep 27, 2021, 4:58 AM
S
Hi
user G
, I my requirement is that the first 8 documents should not appear in any other querySep 27, 2021, 5:38 AM
D
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:
& you’ll see that they don’t overlap
*[_type == "post" && featured != true] | order(publishedAt desc) [0...8]
Sep 27, 2021, 5:38 AM
S
is there any way to reference slug instead of catId?
Sep 27, 2021, 5:41 AM
S
is there any way to reference slug instead of catId?
Sep 27, 2021, 5:41 AM
S
the post references one or more categories as an array
Sep 27, 2021, 5:42 AM
D
Yeah I think so, you’d need to query for the
_idfrom 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
D
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.