0

I'm trying to access all the items that does not have Featured in their category array. The GROQ code I use is

`*[_type == "post"  && *[_type == "category" &&title != "Featured"][0]._id in categories[]._ref] {title, 'mainImage' : mainImage.asset->url, publishedAt, excerpt, slug, categories}[0...6]`

But I'm getting 6 posts including ones with Featured category. What's going wrong here?

Sachin Titus
  • 1,960
  • 3
  • 23
  • 41

2 Answers2

0

I could figure out the solution using Sanity's Vision plugin.

`*[_type == "post"&& !(*[_type == "category"&&title=="Featured"][0]._id in categories[]._ref)]{excerpt, "mainImage":mainImage.asset->url, slug}`
Sachin Titus
  • 1,960
  • 3
  • 23
  • 41
0

Sachin Titus while your solution works it's possible to simplify it:

*[_type == "post" && !("Featured" in categories[]->.title)] {
  excerpt, "mainImage":mainImage.asset->url, slug
}

in this query, we're looking through all documents only once, while in yours you do it twice (see the * which means "all documents") so potentially this should be better optimized and a bit better for reading

Oleg Pro
  • 2,323
  • 1
  • 15
  • 23