一个插件创建了许多我现在需要删除的帖子。它们可按类别定位(例如。Tweet
). 许多还附有图片。
我为它写了一个php脚本*,但没有分页,它就会挂起,因为它做了很多工作(逐个删除帖子)。-而且分页(比如说每页1000页)仍然需要几秒钟,我必须刷新>50次。。。
我认为一个好的数据库查询可以同时选择和删除多篇文章,但是我还想删除数据库和实际文件中所有连接的媒体。
或者,分批处理删除(即使用分页),并可能在两者之间刷新输出,以显示其是否正常工作的更新。
以下是我目前掌握的情况:
$twitter = get_cat_ID( \'Tweet\' );
$instagram = get_cat_ID( \'Instagram\' );
$args = array(
\'category__in\' => array(
$twitter,
$instagram
),
\'post_status\' => \'any\',
\'posts_per_page\' => 1000
);
$social_posts = new WP_Query( $args );
if ( $social_posts->have_posts() ) : ?>
<h1>Deleting the following:</h1>
<ul>
<?php while ( $social_posts->have_posts() ) :
$social_posts->the_post();
?>
<li>
<h5><?php the_title() ?></h5>
<div class=\'post-content\'><?php the_content(); ?></div>
</li>
<?php
$post_id = get_the_ID();
$attachments = get_attached_media( \'\', $post_id ); // \'\' => all attachment Mime types
foreach ( $attachments as $attachment ) {
wp_delete_attachment( $attachment->ID, true );
}
wp_delete_post( $post_id, true );
endwhile;
else: ?>
<p>Oops, there are no posts.</p>
<?php
endif;
(本地测试,放入page-uninstall-social.php
并为其创建了一个页面,以便在我访问该页面时运行)