Including author name and image in GROQ query for multiple types
You need to add a projection to your query that includes the author reference. Here's how to modify your query:
*[_type in ["homePage", "page", "article", "case"] && defined(slug.current)]{
"slug": slug.current,
author->{
name,
image
}
}The key changes:
Replace the
[]with{}- The[]at the end was just selecting the slug values directly. You need a projection (curly braces) to shape the data and include multiple fields.Use the
->operator - This dereferences the author reference, following it to the actual author document and pulling the fields you need.Rename slug for clarity - Since you're now returning an object with multiple fields, I've used
"slug": slug.currentto make it explicit, though you could also just useslugif you want the whole slug object.
If you want to keep the flat array of just slug strings AND get author data, you'd need to decide on your data structure. Most likely you want something like:
*[_type in ["homePage", "page", "article", "case"] && defined(slug.current)]{
slug,
author->{
name,
image
}
}This returns an array of objects, each containing the full slug object and the dereferenced author data. The -> operator is what makes reference dereferencing work in GROQ projections - it follows the reference and lets you select specific fields from the referenced document.
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.