Issue with filtering documents by type in Sanity.io

4 replies
Last updated: Nov 3, 2023
Hey ! I have a question about
documentList().filter()

https://www.sanity.io/docs/structure-builder-reference#filter-c0dc69f8c387
The documentation specify I can use any GROQ filter, but if I specify a type, the query always use the first one. for exemple :


documentList().filter('_type=="type1" || _type=="type2"')
=> only type1 documents in the list

documentList().filter('_type=="type2" || _type=="type1"')
=> only type2 documents in the list
Nov 3, 2023, 2:35 PM
Of course running the filter in the vision tool list all documents
Nov 3, 2023, 2:43 PM
Can you try
_type in ["type1", "type2"]I don't see anything wrong with your version but sometimes I have more success with that as an alternative, even in Vision

Also it may help to have the other bits filled out like
.title() or to include the types as params.
Also the console log may have an error handy and when it does it's usually more readable than other computer errors, as far as schema-related things are concerned.
Nov 3, 2023, 2:47 PM
awesome, it works (and it's clever)
Nov 3, 2023, 2:49 PM
Glad to hear it! Even in regular queries I sometimes do that if I have two types that share a lot of elements. I will use "in" to make sure they're all considered, and then conditional queries on the inside to handle their subtle differences.
Nov 3, 2023, 3:28 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?