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

525 Views Asked by At

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,
},
1

There are 1 best solutions below

2
Oskar Meljon On BEST ANSWER

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