返回介绍

wp_check_for_changed_slugs()

发布于 2017-09-11 11:41:20 字数 3057 浏览 951 评论 0 收藏 0

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

Check 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.


参数

$post_id

(int) (Required) Post ID.

$post

(WP_Post) (Required) The Post Object

$post_before

(WP_Post) (Required) The Previous Post Object


源代码

File: wp-includes/post.php

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' === get_post_type( $post ) && '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 ) ) {
		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 );
	}
}

更新日志

Versiondescription
2.1.0Introduced.

相关函数

Uses

  • wp-includes/post.php: get_post_meta()
  • wp-includes/post.php: add_post_meta()
  • wp-includes/post.php: delete_post_meta()
  • wp-includes/post.php: get_post_type()
  • wp-includes/post.php: is_post_type_hierarchical()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by nothin7

    Typo: “The most logically usage”

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文