函数文档

get_lastpostdate()

💡 云策文档标注

概述

get_lastpostdate() 函数用于获取网站上最新发布文章的时间。它支持指定时区和文章类型,并返回格式化的日期字符串或失败时的 false。

关键要点

  • 函数返回最新发布文章的日期,格式为 'Y-m-d H:i:s',失败时返回 false。
  • 参数 $timezone 可选,接受 'server'、'blog' 或 'gmt',分别对应服务器时区、站点时区和 GMT 时区,默认值为 'server'。
  • 参数 $post_type 可选,指定要检查的文章类型,默认值为 'any'。
  • 函数内部调用 _get_last_post_time() 并应用 get_lastpostdate 过滤器,允许开发者自定义输出。

代码示例

function get_lastpostdate( $timezone = 'server', $post_type = 'any' ) {
	$lastpostdate = _get_last_post_time( $timezone, 'date', $post_type );

	/**
	 * Filters the most recent time that a post on the site was published.
	 *
	 * @since 2.3.0
	 * @since 5.5.0 Added the `$post_type` parameter.
	 *
	 * @param string|false $lastpostdate The most recent time that a post was published,
	 *                                   in 'Y-m-d H:i:s' format. False on failure.
	 * @param string       $timezone     Location to use for getting the post published date.
	 *                                   See get_lastpostdate() for accepted `$timezone` values.
	 * @param string       $post_type    The post type to check.
	 */
	return apply_filters( 'get_lastpostdate', $lastpostdate, $timezone, $post_type );
}

注意事项

  • 时区选项 'blog' 使用 post_date 字段,代理站点设置的时区;'gmt' 使用 post_date_gmt 字段。
  • 函数自 0.71 版本引入,4.4.0 版本添加了 $post_type 参数。
  • 相关函数包括 get_lastpostmodified() 和 _get_last_post_time(),可用于获取修改时间或更通用的时间戳。

📄 原文内容

Retrieves the most recent time that a post on the site was published.

Description

The server timezone is the default and is the difference between GMT and server time. The ‘blog’ value is the date when the last post was posted.
The ‘gmt’ is when the last post was posted in GMT formatted date.

Parameters

$timezonestringoptional
The timezone for the timestamp. Accepts 'server', 'blog', or 'gmt'.
'server' uses the server’s internal timezone.
'blog' uses the post_date field, which proxies to the timezone set for the site.
'gmt' uses the post_date_gmt field.
Default 'server'.
$post_typestringoptional
The post type to check. Default 'any'.

Return

string The date of the last post, or false on failure.

Source

function get_lastpostdate( $timezone = 'server', $post_type = 'any' ) {
	$lastpostdate = _get_last_post_time( $timezone, 'date', $post_type );

	/**
	 * Filters the most recent time that a post on the site was published.
	 *
	 * @since 2.3.0
	 * @since 5.5.0 Added the `$post_type` parameter.
	 *
	 * @param string|false $lastpostdate The most recent time that a post was published,
	 *                                   in 'Y-m-d H:i:s' format. False on failure.
	 * @param string       $timezone     Location to use for getting the post published date.
	 *                                   See get_lastpostdate() for accepted `$timezone` values.
	 * @param string       $post_type    The post type to check.
	 */
	return apply_filters( 'get_lastpostdate', $lastpostdate, $timezone, $post_type );
}

Hooks

apply_filters( ‘get_lastpostdate’, string|false $lastpostdate, string $timezone, string $post_type )

Filters the most recent time that a post on the site was published.

Changelog

Version Description
4.4.0 The $post_type argument was added.
0.71 Introduced.