Querying multiple authors in Sanity using GROQ
Yes! This is absolutely possible in GROQ. You can access array elements by their index position and filter on specific positions within an array.
Here's how you would write that query:
*[_type == "post" && count(authors) > 1 && authors[1]->name == "Chris"]Let me break down what's happening:
count(authors) > 1- Filters for posts with more than one authorauthors[1]- Accesses the second element in the authors array (arrays are zero-indexed, so[1]is the second position)->name == "Chris"- Dereferences the author reference and checks if the name equals "Chris"
If you want to return specific fields from the matching posts, you can add a projection:
*[_type == "post" && count(authors) > 1 && authors[1]->name == "Chris"]{
title,
authors[]->{name}
}A few important notes:
- Array indexing is zero-based:
authors[0]is the first author,authors[1]is the second, etc. - The
->operator dereferences the reference to get the actual author document - You can chain conditions: Combine multiple filters with
&&to get exactly what you need
If your authors array contains objects rather than references, you'd adjust slightly:
*[_type == "post" && count(authors) > 1 && authors[1].name == "Chris"]This pattern works for any array position - just change the index number to match the position you're interested in!
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.