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

Using GROQ to Filter Out a List of Pages to Exclude From slug.current

9 replies
Last updated: Dec 1, 2022
Good day 🙂I’m trying to use GROQ to filter out a
list of pages to exclude if the
slug.current
contain either
/press
or
/blog
Using
match
because the
slug.current
can be
/press/XXX
or
/blog/XXX

The following works

.filter('_type == "page" && !(slug.current match "/press") && !(slug.current match "/blog")')

I’m looking into simplified the match to either but the following doesn’t work. Any advice?

.filter('_type == "page" && !(slug.current match ["/press", "/blog"])')
Sep 13, 2022, 10:44 AM
!(slug.current in ["/press", "/blog"])
Sep 13, 2022, 10:48 AM
Using
match
because the
slug.current
can be
/press/XXX
or
/blog/XXX
Sep 13, 2022, 10:52 AM
Hmmm, not sure how it works with arrays, but you could check
path


[!(slug.current in path("/press.**"))] // slug.current matches anything that is not under the "/press" slug
Not much different from
match
in this instance, but maybe it has some array matching
Sep 13, 2022, 10:57 AM
Didn’t work 🙂
Sep 13, 2022, 11:08 AM
The
path
function only works on the
_id
field.
I don’t think there is a way to make that query simpler right now I’m afraid.
Sep 13, 2022, 11:51 AM
Thanks for confirming
Sep 13, 2022, 11:53 AM
Hi
user P
,Have you solve your problem ?
I am stuck with the same problem
😕
Nov 29, 2022, 7:20 AM
user M
match
can’t be simplified to support multiple array. End up having 2 matches with AND operator
.filter('_type == "page" && !(slug.current match "/press") && !(slug.current match "/blog")')
Nov 30, 2022, 12:57 PM
Thank you
user P
But problem is I have a .(dot) in the matching path and it does not work with .
Dec 1, 2022, 5:28 AM

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?