wp_check_for_changed_dates()
云策文档标注
概述
wp_check_for_changed_dates() 函数用于在更新已发布的文章对象时,检查日期是否变更,并将旧日期保存到文章元数据中。其主要应用场景是支持文章日期变更后的重定向,确保旧链接能正确指向新文章。
关键要点
- 函数在文章更新时被调用,通过比较当前和之前的文章对象来检测日期变化。
- 仅处理已发布的非层级文章类型(如标准文章),附件类型需为“inherit”状态。
- 如果日期变更且旧日期未在 '_wp_old_date' 元数据中,则添加该旧日期;如果新日期已存在于旧日期列表中,则删除它。
- 函数返回 void,无参数时直接返回。
代码示例
function wp_check_for_changed_dates( $post_id, $post, $post_before ) {
$previous_date = gmdate( 'Y-m-d', strtotime( $post_before->post_date ) );
$new_date = gmdate( 'Y-m-d', strtotime( $post->post_date ) );
// Don't bother if it hasn't changed.
if ( $new_date === $previous_date ) {
return;
}
// We're only concerned with published, non-hierarchical objects.
if ( ! ( 'publish' === $post->post_status || ( 'attachment' === $post->post_type && 'inherit' === $post->post_status ) )
|| is_post_type_hierarchical( $post->post_type )
) {
return;
}
$old_dates = (array) get_post_meta( $post_id, '_wp_old_date' );
// If we haven't added this old date before, add it now.
if ( ! empty( $previous_date ) && ! in_array( $previous_date, $old_dates, true ) ) {
add_post_meta( $post_id, '_wp_old_date', $previous_date );
}
// If the new slug was used previously, delete it from the list.
if ( in_array( $new_date, $old_dates, true ) ) {
delete_post_meta( $post_id, '_wp_old_date', $new_date );
}
}注意事项
- 函数自 WordPress 4.9.3 版本引入,开发者需确保在兼容版本中使用。
- 相关函数包括 add_post_meta()、delete_post_meta()、is_post_type_hierarchical() 和 get_post_meta(),用于元数据操作和类型判断。
原文内容
Checks for changed dates for published post objects and save the old date.
Description
The function is used when a post object of any type is updated, by comparing the current and previous post objects.
If the date was changed and not already part of the old dates then it will be added to the post meta field (‘_wp_old_date’) for storing old dates for that post.
The most logically usage of this function is redirecting changed post objects, so that those that linked to an changed post will be redirected to the new post.
Parameters
Source
function wp_check_for_changed_dates( $post_id, $post, $post_before ) {
$previous_date = gmdate( 'Y-m-d', strtotime( $post_before->post_date ) );
$new_date = gmdate( 'Y-m-d', strtotime( $post->post_date ) );
// Don't bother if it hasn't changed.
if ( $new_date === $previous_date ) {
return;
}
// We're only concerned with published, non-hierarchical objects.
if ( ! ( 'publish' === $post->post_status || ( 'attachment' === $post->post_type && 'inherit' === $post->post_status ) )
|| is_post_type_hierarchical( $post->post_type )
) {
return;
}
$old_dates = (array) get_post_meta( $post_id, '_wp_old_date' );
// If we haven't added this old date before, add it now.
if ( ! empty( $previous_date ) && ! in_array( $previous_date, $old_dates, true ) ) {
add_post_meta( $post_id, '_wp_old_date', $previous_date );
}
// If the new slug was used previously, delete it from the list.
if ( in_array( $new_date, $old_dates, true ) ) {
delete_post_meta( $post_id, '_wp_old_date', $new_date );
}
}
Changelog
| Version | Description |
|---|---|
| 4.9.3 | Introduced. |