我需要将某个类别(包含数千个帖子)的所有帖子导出到文本文档。然后有人会在这个文档中进行更正和更改,之后我必须将所有更新的帖子输入到 WP。
所以我决定最好的方法是制作一个 XML 文档(通过这种方式可以很容易地输入回帖)。
所以我的代码是:
require_once(dirname(__FILE__) . '/wp-blog-header.php');
$counter = 0;
$recorded = array();
$double=0;
$handle = fopen("all_posts.xml", "w");
fwrite($handle, "<all_posts>" . "\r\n"); // the root XML tag
// get all the categories from the global category
$global_cat = get_categories(array("child_of"=>5, 'pad_counts'=>true, 'hierarchical' =>
false));
foreach($global_cat as $child_cat){
global $post;
$args = array('numberposts' => 50000,'cat' => $child_cat->cat_ID);
print_r($child_cat); echo "<br>" . $counter ."<br>";
$q_posts = get_posts($args);
foreach($q_posts as $post){
setup_postdata($post);
if( in_array($post->ID, $recorded ) ) {continue;}
$recorded[] = $post->ID;
$counter++;
$title = get_the_title();
$cur_categories = get_the_category();
$cur_tags = get_the_tags();
$d = get_the_date();
$cont = get_the_content();
fwrite($handle, "<post>" . "\r\n");
fwrite($handle, "<title>" . $title . "</title>" . "\r\n");
fwrite($handle, "<id>" . $post->ID . "</id>" . "\r\n");
fwrite($handle, "<cur_cat>" . $child_cat->name . "</cur_cat>" . "\r\n");
fwrite($handle, "<categories>\r\n");
foreach ($cur_categories as $cat) {
fwrite($handle, "<cat>" . $cat->cat_name . "</cat>");
}
fwrite($handle, "\r\n</categories>" . "\r\n");
fwrite($handle, "<tags>\r\n");
foreach ($cur_tags as $tag) {
fwrite($handle, "<tag>" . $tag->name . "</tag>");
}
fwrite($handle, "\r\n</tags>" . "\r\n");
fwrite($handle, "<date>" . $d . "</date>\r\n");
fwrite($handle, "<content>\r\n" . $cont . "</content>\r\n\r\n");
fwrite($handle, "</post>" . "\r\n");
}
}
fwrite($handle, "</all_posts>");
fclose($handle);
问题是,因为有10,000这样的东西,服务器没有响应[我认为是因为xml文件变得很大或者因为procceng php脚本的时间过长]。只有当我尝试从只有 2000 个帖子的类别中导出帖子时,它才能正常工作。
修复它的方法是什么?