如何使用Sanity限制查询结果仅包括最新的3篇帖子?

huangapple go评论57阅读模式
英文:

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

问题

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

英文:

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

得分: 1

你可以通过在查询末尾添加带有开始和结束索引的数组来切片查询,所以在你的情况下,它会看起来像这样:

"latestArticles": *[_type == "article" && wasDeleted != true && isDraft != true] | order(publishDate desc){
  title,
  _createdAt
}[0...2] // 获取条目 0、1 和 2(总共 3 个)

有一个专门介绍这个的部分在 sanity [文档][1]

[1]: https://www.sanity.io/docs/how-queries-work#16b34b599b2f
英文:

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

huangapple
  • 本文由 发表于 2023年4月13日 14:39:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定