sanitygroq

How to limit query to 3 most recent posts with sanity?


I am trying to query the 3 most recent article posts but not sure how to set the limit

"latestArticles": *[_type == "article" && wasDeleted != true && isDraft != true] | order(publishDate desc){
  title,
  _createdAt,
},

Solution

  • You can slice your query by adding array with start and end index at the end of query, so it would look like this in your case:

    "latestArticles": *[_type == "article" && wasDeleted != true && isDraft != true] | order(publishDate desc){
      title,
      _createdAt
    }[0...2] // get entries 0, 1, and 2 (3 total)
    

    There is section dedicated to this in sanity documentation