函数文档

get_lastpostmodified()

💡 云策文档标注

概述

get_lastpostmodified() 函数用于获取站点中文章的最新修改时间,支持指定时区和文章类型,返回格式化的时间戳或 false。

关键要点

  • 函数返回最近修改文章的时间,格式为 'Y-m-d H:i:s',失败时返回 false。
  • 参数 $timezone 可选,接受 'server'(服务器时区)、'blog'(站点时区)或 'gmt'(GMT 时区),默认 'server'。
  • 参数 $post_type 可选,指定要检查的文章类型,默认 'any'。
  • 内部使用 _get_last_post_time() 和 get_lastpostdate() 函数,并包含两个过滤器钩子:pre_get_lastpostmodified 和 get_lastpostmodified。
  • 函数自 WordPress 1.2.0 引入,4.4.0 版本添加了 $post_type 参数。

代码示例

$blog_modify_date = get_lastpostmodified( 'blog' );
$not_formatted_date = strtotime( $blog_modify_date );
$formatted_date = date( 'd/m/Y H:i', $not_formatted_date );
echo $formatted_date;

📄 原文内容

Gets the most recent time that a post on the site was modified.

Description

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

Parameters

$timezonestringoptional
The timezone for the timestamp. See get_lastpostdate() for information on accepted values.
Default 'server'.

More Arguments from get_lastpostdate( … $timezone )

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 timestamp in ‘Y-m-d H:i:s’ format, or false on failure.

Source

function get_lastpostmodified( $timezone = 'server', $post_type = 'any' ) {
	/**
	 * Pre-filter the return value of get_lastpostmodified() before the query is run.
	 *
	 * @since 4.4.0
	 *
	 * @param string|false $lastpostmodified The most recent time that a post was modified,
	 *                                       in 'Y-m-d H:i:s' format, or false. Returning anything
	 *                                       other than false will short-circuit the function.
	 * @param string       $timezone         Location to use for getting the post modified date.
	 *                                       See get_lastpostdate() for accepted `$timezone` values.
	 * @param string       $post_type        The post type to check.
	 */
	$lastpostmodified = apply_filters( 'pre_get_lastpostmodified', false, $timezone, $post_type );

	if ( false !== $lastpostmodified ) {
		return $lastpostmodified;
	}

	$lastpostmodified = _get_last_post_time( $timezone, 'modified', $post_type );
	$lastpostdate     = get_lastpostdate( $timezone, $post_type );

	if ( $lastpostdate > $lastpostmodified ) {
		$lastpostmodified = $lastpostdate;
	}

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

Hooks

apply_filters( ‘get_lastpostmodified’, string|false $lastpostmodified, string $timezone, string $post_type )

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

apply_filters( ‘pre_get_lastpostmodified’, string|false $lastpostmodified, string $timezone, string $post_type )

Pre-filter the return value of get_lastpostmodified() before the query is run.

Changelog

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

User Contributed Notes