The below code is untested | use are your own risk.
非CLI方法可能只是查询ID、循环ID、删除与ID关联的媒体、重定向回自身以进行下一次迭代。
这是基于浏览器的,您需要导航到网站的特定url。它专门寻找delposts
查询变量。
domain.com/?delposts=true
add_action( \'template_redirect\', function() {
if( ! isset( $_GET[\'delposts\'] ) ) {
return;
}
$page = ( isset( $_GET[\'page\'] ) ) ? intval( $_GET[\'page\'] ) : 1;
$twitter = get_cat_ID( \'Tweet\' );
$instagram = get_cat_ID( \'Instagram\' );
$social_posts = get_posts( array(
\'category__in\' => array(
$twitter,
$instagram
),
\'post_status\' => \'any\',
\'posts_per_page\' => 200,
\'fields\' => \'ids\',
) );
if( ! empty( $social_posts ) ) {
foreach( $social_posts as $post_id ) {
$media = get_posts( array(
\'post_parent\' => $post_id,
\'post_type\' => \'attachment\'
\'posts_per_page\'=> 500,
\'fields\' => \'ids\',
) );
if( ! empty( $media ) ) {
foreach( $media as $media_id ) {
wp_delete_attachment( $media_id );
}
}
wp_delete_post( $post_id, true );
}
wp_safe_redirect( add_query_arg( array(
\'delposts\' => true,
\'page\' => $page++,
), home_url() ) );
exit();
}
} );