我知道,这是一个老帖子,但由于我有一个类似的问题,我想在Bendoh的回答中添加一些建议(不幸的是,我没有发表评论的名声)
由于wordpress和一些插件也在post表中存储数据,我建议在您的查询中添加post\\u类型:
global $wpdb;
$duplicate_titles = $wpdb->get_col("SELECT post_title FROM {$wpdb->posts} WHERE `post_type` = \'post\' GROUP BY post_title HAVING COUNT(*) > 1");
foreach( $duplicate_titles as $title ) {
$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_title=%s", $title ) );
// Iterate over the second ID with this post title till the last
foreach( array_slice( $post_ids, 1 ) as $post_id ) {
wp_delete_post( $post_id, true ); // Force delete this post
}
}
根据副本的来源,我还会添加另一个字段来检查副本,如发布日期。
global $wpdb;
$duplicate_titles = $wpdb->get_results("SELECT post_title, post_date, CONCAT(post_date, post_title) AS WHOLENAME FROM {$wpdb->posts} WHERE post_type = \'post\' GROUP BY WHOLENAME HAVING COUNT(WHOLENAME) > 1");
foreach( $duplicate_titles as $title ) {
$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_title=%s AND post_date=\'".$title->post_date."\'", $title->post_title ) );
foreach( array_slice( $post_ids, 1 ) as $post_id ) {
wp_delete_post( $post_id, true ); // Force delete this post
}
}