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
AI Update

I appreciate you sharing the issue you're experiencing with documentList().filter(). This is an interesting behavior that deserves a thorough investigation.

Based on the official Structure Builder documentation, the filter() method accepts "any GROQ filter" with some limitations: it "does not support joins, since they operate on individual documents, and will ignore order-clauses and projections."

However, the documentation doesn't explicitly mention a limitation with OR conditions for multiple _type values. The behavior you're describing—where only the first type in an OR expression appears—isn't documented as expected behavior.

Troubleshooting Steps

First, try the in operator syntax:

documentList().filter('_type in ["type1", "type2"]')

This is the more idiomatic GROQ syntax for checking multiple values and may work better than the || operator in this context.

If that doesn't work, verify your filter is being applied:

Check your browser's network tab to see what query is actually being sent to Sanity. This can help determine whether the issue is with how the filter is constructed or how it's being rendered in the UI.

Alternative Approaches

If the filter continues to show only one type, here are some reliable workarounds:

Option 1: Separate lists in the same pane

S.list()
  .title('Combined Types')
  .items([
    S.documentListItem()
      .title('Type 1')
      .schemaType('type1')
      .child(S.documentList().filter('_type == "type1"')),
    S.documentListItem()
      .title('Type 2')
      .schemaType('type2')
      .child(S.documentList().filter('_type == "type2"')),
  ])

Option 2: Use documentTypeListItem() for each type

S.list()
  .title('My Content')
  .items([
    S.documentTypeListItem('type1'),
    S.documentTypeListItem('type2'),
  ])

Option 3: Custom component with manual GROQ query

If you need a truly unified list, create a custom component view that queries for multiple types directly using the Sanity client.

Reporting the Issue

Since this behavior isn't documented and seems unexpected, I'd recommend reporting it in the Sanity community Slack or as a GitHub issue on the sanity-io/sanity repository. Include your specific filter expression and the observed behavior so the team can investigate whether this is a bug or an undocumented limitation that should be clarified in the docs.

Of course running the filter in the vision tool list all documents
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.
awesome, it works (and it's clever)
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.

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?