✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Get current post, previous post, and next post

By James Singleton & Knut Melvær

This can be used for blogs or articles where you want to possibly display next and previous article buttons

Sanity Query

*[_type == "post" && slug.current == $slug]{
  "currentPost": {
    title
  },
  "previousPost": *[_type == "post" && ^.publishedAt > publishedAt]|order(publishedAt desc)[0]{title,"slug": slug.current},
  "nextPost": *[_type == "post" && ^.publishedAt < publishedAt]|order(publishedAt asc)[0]{title,"slug": slug.current},
  "morePosts": *[_type == "post" && slug.current != $slug] | order(publishedAt desc, _updatedAt desc)[0...3] {
    ${postFields}
  }
}|order(publishedAt)[0]

Contributors

Other schemas by authors