函数文档

_update_blog_date_on_post_publish()

💡 云策文档标注

概述

_update_blog_date_on_post_publish() 是一个 WordPress 函数,用于在文章发布或已发布文章状态变更时,更新站点的最后更新日期。它通过检查文章状态和类型,确保仅对公开文章类型执行更新操作。

关键要点

  • 函数作为处理器,在文章状态从非发布变为发布或反之时触发。
  • 仅处理公开文章类型,通过 get_post_type_object() 验证。
  • 调用 wpmu_update_blogs_date() 更新当前站点的 last_updated 字段。
  • 引入于 WordPress 3.3.0 版本。

代码示例

function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
	$post_type_obj = get_post_type_object( $post->post_type );
	if ( ! $post_type_obj || ! $post_type_obj->public ) {
		return;
	}

	if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
		return;
	}

	// Post was freshly published, published post was saved, or published post was unpublished.

	wpmu_update_blogs_date();
}

注意事项

  • 函数依赖于 wpmu_update_blogs_date() 和 get_post_type_object() 相关函数。
  • 适用于多站点环境,更新当前站点的日期字段。

📄 原文内容

Handler for updating the site’s last updated date when a post is published or an already published post is changed.

Parameters

$new_statusstringrequired
The new post status.
$old_statusstringrequired
The old post status.
$postWP_Postrequired
Post object.

Source

function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
	$post_type_obj = get_post_type_object( $post->post_type );
	if ( ! $post_type_obj || ! $post_type_obj->public ) {
		return;
	}

	if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
		return;
	}

	// Post was freshly published, published post was saved, or published post was unpublished.

	wpmu_update_blogs_date();
}

Changelog

Version Description
3.3.0 Introduced.