wp_add_trashed_suffix_to_post_name_for_trashed_posts()
云策文档标注
概述
wp_add_trashed_suffix_to_post_name_for_trashed_posts() 函数用于检查是否存在已移至回收站的文章使用了指定 slug,并在必要时添加后缀。该函数主要用于内部处理,以确保 slug 的唯一性,并支持文章从回收站恢复时能重新使用原 slug。
关键要点
- 函数检查指定 slug 是否被回收站中的文章占用,忽略指定的 post_id。
- 如果存在匹配的回收站文章,会调用 wp_add_trashed_suffix_to_post_name_for_post() 为这些文章添加后缀。
- 此函数由 wp_insert_post() 调用,用于在插入或更新文章时处理 slug 冲突。
- 自 WordPress 4.5.0 版本引入。
代码示例
function wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_id = 0 ) {
$trashed_posts_with_desired_slug = get_posts(
array(
'name' => $post_name,
'post_status' => 'trash',
'post_type' => 'any',
'nopaging' => true,
'post__not_in' => array( $post_id ),
)
);
if ( ! empty( $trashed_posts_with_desired_slug ) ) {
foreach ( $trashed_posts_with_desired_slug as $_post ) {
wp_add_trashed_suffix_to_post_name_for_post( $_post );
}
}
}
原文内容
Adds a suffix if any trashed posts have a given slug.
Description
Store its desired (i.e. current) slug so it can try to reclaim it if the post is untrashed.
For internal use.
Parameters
$post_namestringrequired-
Post slug.
$post_idintoptional-
Post ID that should be ignored. Default 0.
Source
function wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_id = 0 ) {
$trashed_posts_with_desired_slug = get_posts(
array(
'name' => $post_name,
'post_status' => 'trash',
'post_type' => 'any',
'nopaging' => true,
'post__not_in' => array( $post_id ),
)
);
if ( ! empty( $trashed_posts_with_desired_slug ) ) {
foreach ( $trashed_posts_with_desired_slug as $_post ) {
wp_add_trashed_suffix_to_post_name_for_post( $_post );
}
}
}
Changelog
| Version | Description |
|---|---|
| 4.5.0 | Introduced. |