# wp_old_slug_redirect()

URL: https://chugunov.pro/api-wordpress/functions/wp_old_slug_redirect/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 2.1.0.

## Сигнатура

```php
wp_old_slug_redirect()
```

## Описание

Пытается определить текущий слаг по прежним слагам.

## Исходный код

Файл: `wp-includes/query.php:1055`

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

## История изменений

- 2.1.0 — Introduced.

## Связанные

Использует: [`_find_post_by_old_slug`](https://chugunov.pro/api-wordpress/functions/_find_post_by_old_slug/), [`_find_post_by_old_date`](https://chugunov.pro/api-wordpress/functions/_find_post_by_old_date/), [`is_embed`](https://chugunov.pro/api-wordpress/functions/is_embed/), [`wp_redirect`](https://chugunov.pro/api-wordpress/functions/wp_redirect/), [`is_404`](https://chugunov.pro/api-wordpress/functions/is_404/), [`get_query_var`](https://chugunov.pro/api-wordpress/functions/get_query_var/), `WP_Query`, [`user_trailingslashit`](https://chugunov.pro/api-wordpress/functions/user_trailingslashit/), [`is_post_type_hierarchical`](https://chugunov.pro/api-wordpress/functions/is_post_type_hierarchical/), [`trailingslashit`](https://chugunov.pro/api-wordpress/functions/trailingslashit/), [`get_permalink`](https://chugunov.pro/api-wordpress/functions/get_permalink/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/wp_old_slug_redirect/
