get_feed_build_date()
云策文档标注
概述
get_feed_build_date() 函数用于从 WP_Query 中获取最近修改的文章或评论的 UTC 时间,并返回指定格式的日期字符串。它支持在评论 feed 中处理评论修改时间,并包含回退机制和过滤器钩子。
关键要点
- 函数从 WP_Query 中提取文章和评论的修改时间,返回最大值的 UTC 时间。
- 如果 WP_Query 无效或没有数据,则回退到 get_lastpostmodified('GMT') 获取站点最近修改时间。
- 支持通过 apply_filters('get_feed_build_date', $max_modified_time, $format) 钩子过滤输出结果。
- 参数 $format 为必填,指定返回时间的日期格式字符串。
- 返回值为字符串(成功时)或 false(失败时)。
代码示例
function get_feed_build_date( $format ) {
global $wp_query;
$datetime = false;
$max_modified_time = false;
$utc = new DateTimeZone( 'UTC' );
if ( ! empty( $wp_query ) && $wp_query->have_posts() ) {
$modified_times = wp_list_pluck( $wp_query->posts, 'post_modified_gmt' );
if ( $wp_query->is_comment_feed() && $wp_query->comment_count ) {
$comment_times = wp_list_pluck( $wp_query->comments, 'comment_date_gmt' );
$modified_times = array_merge( $modified_times, $comment_times );
}
$datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', max( $modified_times ), $utc );
}
if ( false === $datetime ) {
$datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', get_lastpostmodified( 'GMT' ), $utc );
}
if ( false !== $datetime ) {
$max_modified_time = $datetime->format( $format );
}
return apply_filters( 'get_feed_build_date', $max_modified_time, $format );
}注意事项
- 函数依赖于全局变量 $wp_query,确保在调用前 WP_Query 已正确初始化。
- 在评论 feed 场景下,会合并文章和评论的修改时间进行比较。
- 使用 date_create_immutable_from_format 处理日期,确保时区为 UTC。
- 过滤器钩子 get_feed_build_date 允许开发者自定义返回的日期值。
原文内容
Gets the UTC time of the most recently modified post from WP_Query.
Description
If viewing a comment feed, the time of the most recently modified comment will be returned.
Parameters
$formatstringrequired-
Date format string to return the time in.
Source
function get_feed_build_date( $format ) {
global $wp_query;
$datetime = false;
$max_modified_time = false;
$utc = new DateTimeZone( 'UTC' );
if ( ! empty( $wp_query ) && $wp_query->have_posts() ) {
// Extract the post modified times from the posts.
$modified_times = wp_list_pluck( $wp_query->posts, 'post_modified_gmt' );
// If this is a comment feed, check those objects too.
if ( $wp_query->is_comment_feed() && $wp_query->comment_count ) {
// Extract the comment modified times from the comments.
$comment_times = wp_list_pluck( $wp_query->comments, 'comment_date_gmt' );
// Add the comment times to the post times for comparison.
$modified_times = array_merge( $modified_times, $comment_times );
}
// Determine the maximum modified time.
$datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', max( $modified_times ), $utc );
}
if ( false === $datetime ) {
// Fall back to last time any post was modified or published.
$datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', get_lastpostmodified( 'GMT' ), $utc );
}
if ( false !== $datetime ) {
$max_modified_time = $datetime->format( $format );
}
/**
* Filters the date the last post or comment in the query was modified.
*
* @since 5.2.0
*
* @param string|false $max_modified_time Date the last post or comment was modified in the query, in UTC.
* False on failure.
* @param string $format The date format requested in get_feed_build_date().
*/
return apply_filters( 'get_feed_build_date', $max_modified_time, $format );
}
Hooks
- apply_filters( ‘get_feed_build_date’, string|false $max_modified_time, string $format )
-
Filters the date the last post or comment in the query was modified.
Changelog
| Version | Description |
|---|---|
| 5.2.0 | Introduced. |