Title: wp_check_for_changed_dates
Published: October 5, 2018
Last modified: February 24, 2026

---

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

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_check_for_changed_dates/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_check_for_changed_dates/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/functions/wp_check_for_changed_dates/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_check_for_changed_dates/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_check_for_changed_dates/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_check_for_changed_dates/?output_format=md#wp--skip-link--target)

Checks for changed dates for published post objects and save the old date.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_check_for_changed_dates/?output_format=md#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](https://developer.wordpress.org/reference/functions/wp_check_for_changed_dates/?output_format=md#parameters)󠁿

 `$post_id`intrequired

Post ID.

`$post`[WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)required

The post object.

`$post_before`[WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)
required

The previous post object.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_check_for_changed_dates/?output_format=md#source)󠁿

    ```php
    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 );
    	}
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/post.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/post.php#L7391)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/post.php#L7391-L7418)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_check_for_changed_dates/?output_format=md#related)󠁿

| Uses | Description | 
| [add_post_meta()](https://developer.wordpress.org/reference/functions/add_post_meta/)`wp-includes/post.php` |

Adds a meta field to the given post.

  | 
| [delete_post_meta()](https://developer.wordpress.org/reference/functions/delete_post_meta/)`wp-includes/post.php` |

Deletes a post meta field for the given post ID.

  | 
| [is_post_type_hierarchical()](https://developer.wordpress.org/reference/functions/is_post_type_hierarchical/)`wp-includes/post.php` |

Determines whether the post type is hierarchical.

  | 
| [get_post_meta()](https://developer.wordpress.org/reference/functions/get_post_meta/)`wp-includes/post.php` |

Retrieves a post meta field for the given post ID.

  |

[Show 1 more](https://developer.wordpress.org/reference/functions/wp_check_for_changed_dates/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_check_for_changed_dates/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_check_for_changed_dates/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.9.3](https://developer.wordpress.org/reference/since/4.9.3/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_check_for_changed_dates%2F)
before being able to contribute a note or feedback.