我刚刚创建了一个函数,可以批量删除没有特定标记的帖子。
您需要具有要保留其帖子的标签的ID。[如果您不知道如何获取这些id:快速解决方案是转到网站管理面板上的标签页。单击标签。在新页面地址栏中,您可以看到标签id]
更换;TagID“;与您的实际标记ID在这一行:
$tags_to_delete = array( $TagID1, $TagID2 , ... );
然后将代码放在主题函数的末尾。php文件。打开主页。它会将所有没有特定标签的帖子移到垃圾箱。
每次在前端打开页面时,都会运行此代码。
运行时取决于post数量和服务器配置。我在配置不太高的服务器上测试了大约3500篇文章,大约花了40秒才完成。
我强烈建议您在完成后从网站中删除代码。
// Change the $TagIDs with the tag IDs in your database
$tags_to_delete = array( $TagID1, $TagID2 , ... );
// This function will bulk delete posts that don\'t have specific tag(s)
function delete_posts_by_not_tag( $tags ) {
// WP_Query arguments
$args = array(
\'post_type\' => array( \'post\' ),
\'nopaging\' => true,
\'posts_per_page\' => \'-1\',
\'tag__not_in\' => array( $tags ),
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
$totalpost = $query->found_posts;
echo "Number of Posts: " . $totalpost;
echo "<hr>";
while ( $query->have_posts() ) {
$query->the_post();
$current_id = $post->ID;
wp_delete_post( $current_id );
echo "Post by ID \'" . $current_id . "\' is deleted.<br>";
}
echo "<hr>";
} else {
echo "No post found to delete!";
}
// Restore original Post Data
wp_reset_postdata();
}
// Run the function to delete all the posts that don\'t have specific tag(s)
delete_posts_by_not_tag( $tags_to_delete );