我最近遇到了一个类似的问题(而是解决如何重置导入后脏的段塞)。这是我用来重置Slug的代码片段:
$wpdb->update(
$wpdb->posts,
array( \'post_name\' => sanitize_title( $post_to_fix->post_title ) ),
array( \'ID\' => $post_to_fix->ID ),
array( \'%s\' ),
array( \'%d\' )
);
主要关注
sanitize_title
, 它在其中清理帖子标题,然后保存到post\\u名称。我相信在你的情况下,如果你对post\\u名称而不是标题进行查询,你将有更好的机会处理标题中的细微差别,并提供准确的匹配。因此,这里粗略介绍一下您可能会在插入后检查/添加逻辑中添加的内容:
// check & set from a valid $_POST property or random string for a post name
$post_name = !empty($_POST[\'post_title\']) ? generateRandomString() : $_POST[\'post_title\'];
// filter by post type, note it can also take an array of post types
$post_type = \'post\';
$get_post_args = array(
\'name\' => $post_name,
\'post_type\' => $post_type,
\'post_status\' => \'any\',
// get all posts
\'posts_per_page\' => -1,
// improve the performance of your query
\'fields\' => \'ids\',
\'no_found_rows\' => true,
\'update_post_term_cache\' => false,
\'update_post_meta_cache\' => false
);
$dup_posts_check = new WP_Query( $get_post_args );
if( !empty($dup_posts_check) ){
echo \'<p style="color:#fff">Sorry, That Title already exists!</p>\';
} else {
//insert the post
}