Pagination issue with groq query for blog posts filtered by author value.

8 replies
Last updated: May 9, 2022
Hello, I’m looking for some help with my groq query. I’m trying to paginate a search for blog posts (type) filtered by author value (slug or name) and am unsure how to do so because I can’t seem to expand that reference in the initial filter.
More in comments…
Apr 4, 2022, 2:45 PM
I did create this query which worked, for initial data, but now I need to paginate.

*[_type == "author" && slug.current == $slug][0]{
  name,  
  "blogs": *[_type == "blog" && author._ref == ^._id] | order(_createdAt desc){
    _id,
    title,
    "slug": slug.current,
    "heroImage": heroImage{
        alt,
        "url": asset->
    },
    "author": author->{
        name,
        "slug": slug.current
    },
    "tags": tags[]->{
        _id,
        title,
        "slug": slug.current
    }
  }
}
Apr 4, 2022, 2:46 PM
I don’t think I can paginate because the value would need to be passed through a template literal.
Apr 4, 2022, 2:47 PM
I had been using template literals as my projection like so
*[_type == "author" && slug.current == $slug][0]{${myProjectionHere}]

But because I am passing a parameter down to the projection, I just went with a regular string
Apr 4, 2022, 8:15 PM
export async function getAuthorPageBySlug(slug, {count}) {
  const results = await sanityClient.fetch(
    `
    *[_type == "author" && slug.current == $slug][0]{
      name,
      "slug": slug.current,
      "blogs": *[_type == "blog" && author._ref == ^._id] | order(_createdAt desc)[${count}...${count + 6}] {
        _id,
        title,
        "slug": slug.current,
        "heroImage": heroImage{
            alt,
            "url": asset->
        },
        "author": author->{
            name,
            "slug": slug.current
        },
        "tags": tags[]->{
            _id,
            title,
            "slug": slug.current
        }
      }
    }
    `,
    { slug }
  );
  return results;
}
Apr 4, 2022, 8:27 PM
Hi Dani. Are you getting any errors or just no results?
Apr 5, 2022, 7:23 PM
Hey Geoff, I did eventually sort it out. I was trying to use template literals and was struggling since that would involve props and probably tagged template literals. I instead just went with a basic string
Apr 5, 2022, 9:44 PM
Thanks for following up. Glad to hear you got it working. 🎉
Apr 5, 2022, 9:47 PM
ace
May 9, 2022, 5:52 AM

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?