wp_check_for_changed_slugs()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
wp_check_for_changed_slugs( int $post_id, WP_Post $post, WP_Post $post_before )
Описание
Функция используется при обновлении объекта записи любого типа и сравнивает текущий и предыдущий объекты записи.
Если слаг изменился и ещё не входит в число старых слагов, он добавляется в метаполе записи ('_wp_old_slug'), где хранятся старые слаги этой записи.
Наиболее логичное применение функции — перенаправление изменившихся объектов записей, чтобы те, кто ссылался на изменившуюся запись, перенаправлялись на новую запись.
Оригинал (английский)
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.
Параметры
$post_id
int
обязательный
$post
WP_Post
обязательный
$post_before
WP_Post
обязательный
Исходный код
wp-includes/post.php:7345
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 );
}
}
История изменений
| Версия | Описание |
|---|---|
| 2.1.0 | Introduced. |

