Get a peek at our latest innovations at Sanity Product Day on Dec 8th →

Get current post, previous post, and next post

By 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": {
  "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] {


Other schemas by author