Querying for all authors and excluding the author of the current article in Sanity.io
13 replies
Last updated: Jan 21, 2022
J
How would one query for all authors and exclude the author of the current article the user is on?
Jan 21, 2022, 10:56 PM
If you have an
If you have an
authorsarray with refs to `person`:
*[
_id == "86e778c1-8e64-41e2-8101-9166efa68a63"
]{
_id,
title,
authors,
"otherAuthors": *[_type == "person" && !(_id in ^.authors[]._ref)]{name}
}authorreference field with ref to `person`:
*[
_id == "86e778c1-8e64-41e2-8101-9166efa68a63"
]{
_id,
title,
author,
"otherAuthors": *[_type == "person" && _id != ^.author._ref]{name}
}Jan 21, 2022, 11:01 PM
If you have an
If you have an
authorsarray with refs to `person`:
*[
_id == "86e778c1-8e64-41e2-8101-9166efa68a63"
]{
_id,
title,
authors,
"otherAuthors": *[_type == "person" && !(_id in ^.authors[]._ref)]{name}
}authorreference field with ref to `person`:
*[
_id == "86e778c1-8e64-41e2-8101-9166efa68a63"
]{
_id,
title,
author,
"otherAuthors": *[_type == "person" && _id != ^.author._ref]{name}
}Jan 21, 2022, 11:01 PM
J
🤔 so right now I am fetching an article via a slug which has an author. So the query looks like
where
So adding _`"otherAuthors": *[_type == "author" && _id != ^.author.
ref]{name},` in the
export const postQuery = groq`
{
'post': *[_type == "post" && slug.current == $slug] | order(_updatedAt desc)[0] {
${postFields}
},
"morePosts": *[_type == "post" && slug.current != $slug] | order(publishedAt desc, _updatedAt desc)[0...4] {
${postFields}
}
}
`postFieldslooks like
const postFields = `
_id,
_updatedAt,
title,
publishedAt,
mainImage,
"categories": categories[]->title,
"slug": slug.current,
"author": author->{name, 'slug': slug.current, image, bio},
excerpt,
body,
featuredArticle
`ref]{name},` in the
postFieldsgives it to me on every post. However, I just wanted to pass the slug in and return the others that didn't write that particular article.
Jan 21, 2022, 11:07 PM
J
🤔 so right now I am fetching an article via a slug which has an author. So the query looks like
where
So adding _`"otherAuthors": *[_type == "author" && _id != ^.author.
ref]{name},` in the
export const postQuery = groq`
{
'post': *[_type == "post" && slug.current == $slug] | order(_updatedAt desc)[0] {
${postFields}
},
"morePosts": *[_type == "post" && slug.current != $slug] | order(publishedAt desc, _updatedAt desc)[0...4] {
${postFields}
}
}
`postFieldslooks like
const postFields = `
_id,
_updatedAt,
title,
publishedAt,
mainImage,
"categories": categories[]->title,
"slug": slug.current,
"author": author->{name, 'slug': slug.current, image, bio},
excerpt,
body,
featuredArticle
`ref]{name},` in the
postFieldsgives it to me on every post. However, I just wanted to pass the slug in and return the others that didn't write that particular article.
Jan 21, 2022, 11:07 PM
So, just a flat array of all other authors, alongside your
postand
morePostsarrays?
Jan 21, 2022, 11:10 PM
J
Yup! My use case is adding a "Discover Authors" section
Jan 21, 2022, 11:11 PM
J
Actually... if I just add it to the single post, that should be fine
Jan 21, 2022, 11:12 PM
J
So like
would probably be the smarter way to do it
export const postQuery = groq`
{
'post': *[_type == "post" && slug.current == $slug] | order(_updatedAt desc)[0] {
${postFields},
"otherAuthors": *[_type == "author" && _id != ^.author._ref]{
name,
'slug': slug.current,
role,
image
}
},
"morePosts": *[_type == "post" && slug.current != $slug] | order(publishedAt desc, _updatedAt desc)[0...4] {
${postFields}
}
}
`Jan 21, 2022, 11:13 PM
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.

