函数文档

wp_check_for_changed_slugs()

💡 云策文档标注

概述

wp_check_for_changed_slugs() 函数用于在更新已发布的文章对象时,检查其 slug 是否发生变化,并将旧 slug 保存到 post meta 字段中,主要用于实现文章重定向。

关键要点

  • 函数在文章更新时被调用,通过比较当前和之前的文章对象来检测 slug 变化。
  • 仅处理已发布(publish 状态)且非层级(non-hierarchical)的文章类型,附件类型需为 inherit 状态。
  • 如果 slug 发生变化且旧 slug 未在 '_wp_old_slug' meta 字段中记录过,则使用 add_post_meta() 添加。
  • 如果新 slug 已存在于旧 slug 列表中,则使用 delete_post_meta() 删除该记录。
  • 核心应用场景是支持文章 slug 变更后的重定向,确保旧链接能正确跳转到新地址。

代码示例

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

注意事项

  • 函数仅适用于非层级文章类型,如 post 或 page(非层级模式),层级类型如页面默认被排除。
  • 参数包括 $post_id(文章 ID)、$post(当前文章对象)和 $post_before(之前文章对象),均为必需。
  • 使用 get_post_meta() 获取旧 slug 列表,确保数据操作准确。
  • 自 WordPress 2.1.0 版本引入,相关函数如 add_post_meta() 和 delete_post_meta() 用于 meta 字段管理。

📄 原文内容

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

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

$post_idintrequired
Post ID.
$postWP_Postrequired
The post object.
$post_beforeWP_Postrequired
The previous post object.

Source

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

Changelog

Version Description
2.1.0 Introduced.

User Contributed Notes