1. 首页
  2. WordPress 函数手册

wp_check_for_changed_slugs()

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

检查已发布的post对象的已更改段塞并保存旧段塞。
Check for changed slugs for published post objects and save the old slug.

目录锚点:#说明#参数#源码#笔记


说明(Description)

此函数用于更新任何类型的post对象时,通过比较当前和以前的post对象。如果更改了段塞而不是旧段塞的一部分,则它将被添加到后置元字段(“u wp_old_slug”)中,以便为该post存储旧段塞。这个函数最合理的用法是重定向已更改的post对象,以便那些链接到已更改的post的对象将被重定向到新的post。


参数(Parameters)

参数 类型 说明
$post_id (int) 职位ID。
$post (WP_Post) Post对象
$post_before (WP_Post) 上一个Post对象

源码(Source)

/**
 * Check for changed slugs for published post objects and save the 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.
 *
 * @since 2.1.0
 *
 * @param int     $post_id     Post ID.
 * @param WP_Post $post        The Post Object
 * @param WP_Post $post_before The Previous Post Object
 */
function wp_check_for_changed_slugs( $post_id, $post, $post_before ) {
	// Don't bother if it hasnt changed.
	if ( $post->post_name == $post_before->post_name )
		return;

	// We're only concerned with published, non-hierarchical objects.
	if ( $post->post_status != 'publish' || 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) )
		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) )
		delete_post_meta($post_id, '_wp_old_slug', $post->post_name);
}
更新版本 源码位置 使用 被使用
2.1.0 wp-includes/post.php 7 15

笔记(Notes)

排版:“最符合逻辑的用法”

wp_check_for_changed_slugs() 为WP2原创文章,链接:https://www.wp2.cn/functions/wp_check_for_changed_slugs/