function wp_check_for_changed_slugs($post_id, $post, $post_before) {}

Checked for changed slugs for published post objects and save the old slug.
The function is used when a post object of any type is updated, by comparing the current and previous post objects.


  • int $post_id: Post ID.
  • object $post: The Post Object
  • object $post_before: The Previous Post Object

Return values

returns:Same as $post_id

Source code

function wp_check_for_changed_slugs($post_id, $post, $post_before) {

	// dont bother if it hasnt changed

	if ( $post->post_name == $post_before->post_name )


	// we're only concerned with published, non-hierarchical objects

	if ( $post->post_status != 'publish' || is_post_type_hierarchical( $post->post_type ) )


	$old_slugs = (array) get_post_meta($post_id, '_wp_old_slug');

	// if we haven't added this old slug before, add it now

	if ( !empty( $post_before->post_name ) && !in_array($post_before->post_name, $old_slugs) )

		add_post_meta($post_id, '_wp_old_slug', $post_before->post_name);

	// if the new slug was used previously, delete it from the list

	if ( in_array($post->post_name, $old_slugs) )

		delete_post_meta($post_id, '_wp_old_slug', $post->post_name);



No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: