Groq query to exclude items from an array within a document type in Sanity

15 replies
Last updated: Jan 30, 2023
hey, hoping someone can give me a hand with something that i feel should be really simple but I just cant get it to work!I have a type called 'press' and then a type called 'pressFeatured' which has an array within it of references to press items.
I want to do a groq query to get back all the press items not including the ones in this featured array. I have tried this:

*[_type == 'press' && references(*[_type == 'pressFeatured' && featured[]._ref != ^.id])]
but it just returns all of the press articles, including the featured one! Anyone got any ideas?
Jan 25, 2023, 11:34 AM
I think you want
!(^.id in featured[].ref])
Jan 25, 2023, 12:56 PM
user L
, thanks for getting back to me! I've just tried
*[_type == 'press' && references(*[_type == 'pressFeatured' && !(^.id in featured[].ref)])]
but it returns no results? Am I missing something?
Jan 25, 2023, 1:06 PM
Hm, I had a typo, you need
. But that might not be why.
Jan 25, 2023, 2:00 PM
ah sorry, I completely missed that 🤦
*[_type == 'press' && references(*[_type == 'pressFeatured' && !(^._id in featured[]._ref)])]
added the underscores to if and ref, but still no results when I'm expecting 154
Jan 25, 2023, 3:17 PM
Hm, I’m not sure, then. If you give me your project ID and dataset name, I can test myself.
Jan 25, 2023, 3:58 PM
Course, I'll dm you
Jan 25, 2023, 4:09 PM
Do you have an example of a document of type
that references a
Jan 25, 2023, 8:36 PM
No sorry its the other way round, so pressFeatured references press documents
Jan 26, 2023, 2:18 PM
works on the document you are filtering. So your query above means “where type is ‘press’ and it references some other documents”.
Jan 26, 2023, 2:20 PM
I’m not sure what you want, then, given your correction. What do this mean:
I want to do a groq query to get back all the press items not including the ones in this featured array.
Jan 26, 2023, 2:21 PM
Is there more than one
Jan 26, 2023, 2:21 PM
Sorry, I must of explained it badly! So I am trying to get back an array of
_type == 'press'
without any that are listed as featured in the document
There is only one document called
with the array of featured items within it, and there will only ever be 1 of these documents.
Jan 26, 2023, 2:33 PM
Ok, then I think you want to something like:
*[_type == 'press' && _id not in *[_type == 'pressFeatured'][0].featured[]._ref]
Jan 26, 2023, 2:34 PM
That gave me this error, but I changed it round to be
*[_type == 'press' && !(_id in *[_type == 'pressFeatured'][0].featured[]._ref)]
and it worked perfectly! thank you!
Jan 30, 2023, 11:01 AM
D’oh, I’ve been doing too much SQL lately. Yes, that’s it. 👍
Jan 30, 2023, 1:07 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the 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?