最近,我不得不为一个拥有34K篇帖子和75K条评论的博客进行导出。我可以通过修改WordPress Advanced Export plugin 以及在php中增加max\\u execution\\u时间。ini从30秒到大约5分钟。
插件获取数据库中所有post ID的快照,然后以20个post增量运行批处理,并循环返回,直到完成。我将增量改为40,这加快了速度,并允许在合理的时间内完成。
以下是如何运行查询的示例:
下面的$where子句由一长串基于插件选项的if-else条件定义。
$post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts $where ORDER BY post_date_gmt ASC");
$pass = 0;
$passes = 1000 + count($categories);
while ( ( $cat = array_shift($categories) ) && ++$pass < $passes ) {
if ( $cat->parent == 0 || isset($cats[$cat->parent]) ) {
$cats[$cat->term_id] = $cat;
} else {
$categories[] = $cat;
}
}
unset($categories);
<?php if ($post_ids) {
global $wp_query;
$wp_query->in_the_loop = true; // Fake being in the loop.
// fetch 20 posts at a time rather than loading the entire table into memory
while ( $next_posts = array_splice($post_ids, 0, 20) ) {
$where = "WHERE ID IN (".join(\',\', $next_posts).")";
$posts = $wpdb->get_results("SELECT * FROM $wpdb->posts $where ORDER BY post_date_gmt ASC");
foreach ($posts as $post) {
setup_postdata($post); ?>