我正在使用该功能从WordPress网站批量删除帖子wp_delete_post
和类别使用wp_delete_category
. 这大约是100000篇帖子和4000个类别,过了一段时间,我得到了一个PHP错误
PHP Fatal error: Allowed memory size of ... bytes exhausted
这就是我在代码中所做的:
// get all IDs from the posts and categories
$delete_categories = $wpdb->get_col("SELECT DISTINCT(j.term_id)...");
$delete_posts = $wpdb->get_col("SELECT DISTINCT(j.post_id)...");
$delete_parent_categories = $wpdb->get_col("SELECT tt.parent FROM $tbl_term_taxonomy tt ...");
// I removed the SQL statements for a better reading
foreach ($delete_posts as $dp) {
echo "deleting post ".$dp."\\n";
wp_delete_post($dp, true);
unset($dp);
}
unset($delete_posts);
foreach ($delete_categories as $dc) {
echo "deleting category ".$dc."\\n";
wp_delete_category($dc);
unset($dc);
}
unset($delete_categories);
foreach ($delete_parent_categories as $dc) {
echo "deleting category ".$dc."\\n";
wp_delete_category($dc);
unset($dc);
}
unset($delete_parent_categories);
删除帖子是可行的,删除某些类别也是可行的——但在某个时候,内存限制被超过,脚本被终止。将内存限制设置为1024MB对我来说似乎不是一个完美的解决方案。
有没有办法释放WordPress功能分配的内存?