How to query multiple _types in Sanity?
You can query multiple _type values using the in operator in GROQ! The syntax looks like this:
*[_type in ["article", "category"]]This will return all documents where the _type is either "article" OR "category". The in operator checks if a value exists in an array, which is exactly what you need for filtering by multiple document types.
Here's a more complete example with a projection:
*[_type in ["post", "page", "product"]]{
_id,
_type,
title,
slug
}This query fetches all documents that are either posts, pages, or products, and returns their ID, type, title, and slug fields.
If you tried using an array directly in the filter like *[_type == ["article", "category"]], that won't work because you're comparing _type to an array rather than checking if it exists within an array. The in operator is the correct way to handle this.
You can also combine this with other filter conditions using the && (AND) or || (OR) operators:
*[_type in ["article", "blogPost"] && publishedAt > "2024-01-01"]The key thing to remember is: use in when you want to check if a field's value matches any value in an array. This is documented in the GROQ pipeline components section, though it can be easy to miss!
Show original thread46 replies
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.