👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Querying for all authors and excluding the author of the current article in Sanity.io

13 replies
Last updated: Jan 21, 2022
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
authors
array with refs to `person`:
*[
  _id == "86e778c1-8e64-41e2-8101-9166efa68a63"
]{
  _id,
  title,
  authors,
  "otherAuthors": *[_type == "person" && !(_id in ^.authors[]._ref)]{name}
}
If you have an
author
 reference 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
user Y
beat me to it!
Jan 21, 2022, 11:01 PM
If you have an
authors
array with refs to `person`:
*[
  _id == "86e778c1-8e64-41e2-8101-9166efa68a63"
]{
  _id,
  title,
  authors,
  "otherAuthors": *[_type == "person" && !(_id in ^.authors[]._ref)]{name}
}
If you have an
author
 reference 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
user Y
beat me to it!
Jan 21, 2022, 11:01 PM
🤔 so right now I am fetching an article via a slug which has an author. So the query looks like
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}
    }
  }
`
where
postFields
looks 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
`
So adding _`"otherAuthors": *[_type == "author" && _id != ^.author.
ref]{name},` in the
postFields
gives 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 right now I am fetching an article via a slug which has an author. So the query looks like
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}
    }
  }
`
where
postFields
looks 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
`
So adding _`"otherAuthors": *[_type == "author" && _id != ^.author.
ref]{name},` in the
postFields
gives 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
you mean you want
otherAuthors
to be a property on the root object?
Jan 21, 2022, 11:10 PM
So, just a flat array of all other authors, alongside your
post
and
morePosts
arrays?
Jan 21, 2022, 11:10 PM
🤐
Jan 21, 2022, 11:10 PM
😬
Jan 21, 2022, 11:11 PM
Yup! My use case is adding a "Discover Authors" section
Jan 21, 2022, 11:11 PM
Actually... if I just add it to the single post, that should be fine
Jan 21, 2022, 11:12 PM
So like
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}
    }
  }
`
would probably be the smarter way to do it
Jan 21, 2022, 11:13 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?