如果只需要更改帖子内容,可以通过直接查询数据库来避免get\\u posts/WP\\u查询的开销:
global $wpdb;
$results = $wpdb->get_results("SELECT ID, post_content FROM {$wpdb->posts}");
$total = count($results);
$changed = 0;
foreach($results as $entry){
$new_content = strip_tags($entry->post_content, \'<img><a>\');
if($entry->post_content !== $new_content){
$wpdb->query($wpdb->prepare(
"UPDATE {$wpdb->posts} SET post_content = %s WHERE ID = %s)",
$new_content, $entry->ID));
$changed++;
}
}
printf("Changed %d out of %d posts", $changed, $total);
(先备份db)