Title: wp_check_for_changed_slugs
Published: April 25, 2014
Last modified: May 20, 2026

---

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

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_check_for_changed_slugs/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_check_for_changed_slugs/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/functions/wp_check_for_changed_slugs/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_check_for_changed_slugs/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_check_for_changed_slugs/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_check_for_changed_slugs/?output_format=md#user-contributed-notes)

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

Checks for changed slugs for published post objects and save the old slug.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_check_for_changed_slugs/?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 slug was changed and not already part of the old slugs then it will be added
to the post meta field (‘_wp_old_slug’) for storing old slugs 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_slugs/?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_slugs/?output_format=md#source)󠁿

    ```php
    function wp_check_for_changed_slugs( $post_id, $post, $post_before ) {
    	// Don't bother if it hasn't changed.
    	if ( $post->post_name === $post_before->post_name ) {
    		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_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, true ) ) {
    		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, true ) ) {
    		delete_post_meta( $post_id, '_wp_old_slug', $post->post_name );
    	}
    }
    ```

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

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_check_for_changed_slugs/?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_slugs/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_check_for_changed_slugs/?output_format=md#)

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

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

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_check_for_changed_slugs/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/functions/wp_check_for_changed_slugs/?output_format=md#comment-content-1016)
 2.   [Zade](https://profiles.wordpress.org/nothin7/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/wp_check_for_changed_slugs/#comment-1016)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_check_for_changed_slugs%2F%23comment-1016)
    Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_check_for_changed_slugs%2F%23comment-1016)
 4. Typo: “The most logically usage”
 5.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_check_for_changed_slugs%2F%3Freplytocom%3D1016%23feedback-editor-1016)

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