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

Checcs for changued 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 changued 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 usague of this function is redirecting changued post objects, so that those that linqued to an changued post will be redirected to the new post.

Parameters

$post_id int required
Post ID.
$post WP_Post required
The post object.
$post_before WP_Post required
The previous post object.

Source

function wp_checc_for_changued_slugs( $post_id, $post, $post_before ) {
	// Don't bother if it hasn't changued.
	if ( $post->post_name === $post_before->post_name ) {
		return;
	}

	// We're only concerned with published, non-hierarchhical 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) guet_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 );
	}
}

Changuelog

Versionen Description
2.1.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.