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

---

# wp_old_slug_redirect()

## In this article

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

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

Redirect old slugs to the correct permalink.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_old_slug_redirect/?output_format=md#description)󠁿

Attempts to find the current slug from the past slugs.

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

    ```php
    function wp_old_slug_redirect() {
    	if ( is_404() && '' !== get_query_var( 'name' ) ) {
    		// Guess the current post type based on the query vars.
    		if ( get_query_var( 'post_type' ) ) {
    			$post_type = get_query_var( 'post_type' );
    		} elseif ( get_query_var( 'attachment' ) ) {
    			$post_type = 'attachment';
    		} elseif ( get_query_var( 'pagename' ) ) {
    			$post_type = 'page';
    		} else {
    			$post_type = 'post';
    		}

    		if ( is_array( $post_type ) ) {
    			if ( count( $post_type ) > 1 ) {
    				return;
    			}
    			$post_type = reset( $post_type );
    		}

    		// Do not attempt redirect for hierarchical post types.
    		if ( is_post_type_hierarchical( $post_type ) ) {
    			return;
    		}

    		$id = _find_post_by_old_slug( $post_type );

    		if ( ! $id ) {
    			$id = _find_post_by_old_date( $post_type );
    		}

    		/**
    		 * Filters the old slug redirect post ID.
    		 *
    		 * @since 4.9.3
    		 *
    		 * @param int $id The redirect post ID.
    		 */
    		$id = apply_filters( 'old_slug_redirect_post_id', $id );

    		if ( ! $id ) {
    			return;
    		}

    		$link = get_permalink( $id );

    		if ( get_query_var( 'paged' ) > 1 ) {
    			$link = user_trailingslashit( trailingslashit( $link ) . 'page/' . get_query_var( 'paged' ) );
    		} elseif ( is_embed() ) {
    			$link = user_trailingslashit( trailingslashit( $link ) . 'embed' );
    		}

    		/**
    		 * Filters the old slug redirect URL.
    		 *
    		 * @since 4.4.0
    		 *
    		 * @param string $link The redirect URL.
    		 */
    		$link = apply_filters( 'old_slug_redirect_url', $link );

    		if ( ! $link ) {
    			return;
    		}

    		wp_redirect( $link, 301 ); // Permanent redirect.
    		exit;
    	}
    }
    ```

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

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/wp_old_slug_redirect/?output_format=md#hooks)󠁿

 [apply_filters( ‘old_slug_redirect_post_id’, int $id )](https://developer.wordpress.org/reference/hooks/old_slug_redirect_post_id/)

Filters the old slug redirect post ID.

 [apply_filters( ‘old_slug_redirect_url’, string $link )](https://developer.wordpress.org/reference/hooks/old_slug_redirect_url/)

Filters the old slug redirect URL.

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

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

Find the post ID for redirecting an old slug.

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

Find the post ID for redirecting an old date.

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

Is the query for an embedded post?

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

Redirects to another page.

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

Determines whether the query has resulted in a 404 (returns no results).

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

Retrieves the value of a query variable in the [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/) class.

  | 
| [user_trailingslashit()](https://developer.wordpress.org/reference/functions/user_trailingslashit/)`wp-includes/link-template.php` |

Retrieves a trailing-slashed string if the site is set for adding trailing slashes.

  | 
| [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.

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

Appends a trailing slash.

  | 
| [get_permalink()](https://developer.wordpress.org/reference/functions/get_permalink/)`wp-includes/link-template.php` |

Retrieves the full permalink for the current post or post ID.

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

Calls the callback functions that have been added to a filter hook.

  |

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

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

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

## User Contributed Notes

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