Skip to main content

文档服务 API:草稿与发布的使用

🌐 Document Service API: Usage with Draft & Publish

Page summary:

使用 Document Service API 的 status 参数可以检索文档的已发布或草稿版本,按状态统计文档数量,并在创建或更新文档时直接发布文档。

🌐 Use the status parameter with the Document Service API to retrieve published or draft versions of documents, count documents by status, and directly publish documents during creation or updates.

默认情况下,当启用 Draft & Publish 功能时,Document Service API 会返回文档的草稿版本。此页面描述了如何使用 status 参数来:

🌐 By default the Document Service API returns the draft version of a document when the Draft & Publish feature is enabled. This page describes how to use the status parameter to:

  • 返回文档的已发布版本,
  • 根据文档的状态计数文档,
  • 并在创建或更新文档时直接发布文档。
Note

{ status: 'draft' } 传递给文档服务 API 查询返回的结果与未传递任何 status 参数时相同。

🌐 Passing { status: 'draft' } to a Document Service API query returns the same results as not passing any status parameter.

要根据文档的草稿版本和已发布版本的关系(从未发布、已修改等)选择文档,请参见 Document Service API: publicationFilter

🌐 To select documents by how their draft and published versions relate (never-published, modified, and others), see Document Service API: publicationFilter.

获取已发布的版本与 findOne()

🌐 Get the published version with findOne()

strapi.documents().findOne()

findOne() with status: 'published'

Return the published version of a specific document.

获取带有 findFirst() {#find-first} 的已发布版本

🌐 Get the published version with findFirst()

strapi.documents().findFirst()

findFirst() with status: 'published'

Return the published version of the first matching document.

获取已发布的版本与 findMany()

🌐 Get the published version with findMany()

strapi.documents().findMany()

findMany() with status: 'published'

Return the published versions of all matching documents.

count() 仅草稿或已发布版本

🌐 count() only draft or published versions

在使用文档服务 API 计数文档 时,如果只考虑文档的草稿或已发布版本,请传递相应的 status 参数:

🌐 To take into account only draft or published versions of documents while counting documents with the Document Service API, pass the corresponding status parameter:

// Count draft documents (also actually includes published documents)
const draftsCount = await strapi.documents("api::restaurant.restaurant").count({
status: 'draft'
});
// Count only published documents
const publishedCount = await strapi.documents("api::restaurant.restaurant").count({
status: 'published'
});
Note

由于已发布的文档必然也有草稿副本,因此已发布的文档仍算作具有草稿版本。

🌐 Since published documents necessarily also have a draft counterpart, a published document is still counted as having a draft version.

这意味着,即使某些文档已经发布,并且在内容管理器中不再显示为“草稿”或“已修改”,使用 status: 'draft' 参数进行计数仍会返回匹配其他参数的文档总数。要仅计数从未发布的草稿,请传递 publicationFilter,例如 'never-published''never-published-document'

🌐 This means that counting with the status: 'draft' parameter still returns the total number of documents matching other parameters, even if some documents have already been published and are not displayed as "draft" or "modified" in the Content Manager anymore. To count only never-published drafts, pass a publicationFilter value such as 'never-published' or 'never-published-document'.

创建草稿并发布它

🌐 Create a draft and publish it

strapi.documents().create()

create() with status: 'published'

Create a new document and immediately publish it.

更新草稿并发布它

🌐 Update a draft and publish it

strapi.documents().update()

update() with status: 'published'

Update an existing document and immediately publish it.