检查已发布帖子对象的更改日期并保存旧日期。

原型

wp_check_for_changed_dates( int $post_id, WP_Post $post, WP_Post $post_before )

描述

通过比较当前和先前的post对象,在更新任何类型的post对象时使用该函数。

参数

$post_id (int) (Required) 帖子ID。

$post (WP_Post) (Required) 邮政对象

$post_before (WP_Post) (Required) 上一篇文章对象

源文件

路径:wp-includes/post.php

<?php
...
function wp_check_for_changed_dates( $post_id, $post, $post_before ) {
	$previous_date = date( 'Y-m-d', strtotime( $post_before->post_date ) );
	$new_date = date( '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' === get_post_type( $post ) && '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 ) ) {
		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 ) ) {
		delete_post_meta( $post_id, '_wp_old_date', $new_date );
	}
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/wp_check_for_changed_dates/