wp_add_trashed_suffix_to_post_name_for_post()
云策文档标注
概述
wp_add_trashed_suffix_to_post_name_for_post() 函数用于为指定文章添加“已移至回收站”后缀,并存储原始 slug 以便恢复时使用。此函数仅供内部调用。
关键要点
- 函数接受一个 WP_Post 对象或文章 ID 作为参数,返回更新后的 slug。
- 如果文章 slug 已以 '__trashed' 结尾,则直接返回原 slug。
- 通过 add_post_meta() 将原始 slug 存储到 _wp_desired_post_slug 元字段中。
- 使用 _truncate_post_slug() 截断 slug 至 191 字符,并附加 '__trashed' 后缀。
- 通过 wpdb::update() 更新数据库中的 post_name 字段,并调用 clean_post_cache() 清理缓存。
代码示例
function wp_add_trashed_suffix_to_post_name_for_post( $post ) {
global $wpdb;
$post = get_post( $post );
if ( str_ends_with( $post->post_name, '__trashed' ) ) {
return $post->post_name;
}
add_post_meta( $post->ID, '_wp_desired_post_slug', $post->post_name );
$post_name = _truncate_post_slug( $post->post_name, 191 ) . '__trashed';
$wpdb->update( $wpdb->posts, array( 'post_name' => $post_name ), array( 'ID' => $post->ID ) );
clean_post_cache( $post->ID );
return $post_name;
}注意事项
- 此函数自 WordPress 4.5.0 版本引入,主要用于内部处理,开发者应避免直接调用。
- 相关函数包括 wp_add_trashed_suffix_to_post_name_for_trashed_posts() 和 wp_insert_post()。
原文内容
Adds a trashed suffix for a given post.
Description
Store its desired (i.e. current) slug so it can try to reclaim it if the post is untrashed.
For internal use.
Parameters
$postWP_Postrequired-
The post.
Source
function wp_add_trashed_suffix_to_post_name_for_post( $post ) {
global $wpdb;
$post = get_post( $post );
if ( str_ends_with( $post->post_name, '__trashed' ) ) {
return $post->post_name;
}
add_post_meta( $post->ID, '_wp_desired_post_slug', $post->post_name );
$post_name = _truncate_post_slug( $post->post_name, 191 ) . '__trashed';
$wpdb->update( $wpdb->posts, array( 'post_name' => $post_name ), array( 'ID' => $post->ID ) );
clean_post_cache( $post->ID );
return $post_name;
}
Changelog
| Version | Description |
|---|---|
| 4.5.0 | Introduced. |