所以这些是原始版本(尚未优化),它从当前博客(站点)复制到除#1 之外的所有其他博客(站点)。如果 post_name 存在,则复制内容,否则假定一个新页面,并创建一个。赋予当前登录用户的新页面所有权。
需要进行更改以处理大型安装,就像目前所有操作一样,需要进行一些分页等。
适合我的要求,可能可以在其他情况下使用。所以分享给其他人使用。
$id = get_current_blog_id();
$sql = "SELECT * FROM ".$wpdb->base_prefix."blogs WHERE blog_id != %d AND blog_id > 1";
$blogs = $wpdb->get_results($wpdb->prepare($sql, $id));
foreach ($blogs as $blog):
echo $blog->path.' ('.$blog->blog_id.')<br />';
$blog_id = ($id == 1) ? '' : $id.'_';
$sql = "SELECT * FROM ".$wpdb->base_prefix.$blog_id."posts WHERE post_type='page' and post_status='publish'";
$pages = $wpdb->get_results($sql);
foreach ($pages as $page):
$sql = "SELECT ID FROM ".$wpdb->base_prefix.$blog->blog_id."_posts WHERE post_name = %s AND post_status = 'publish'";
$target = $wpdb->get_row($wpdb->prepare($sql, $page->post_name));
if ($target):
$sql = "UPDATE ".$wpdb->base_prefix.$blog->blog_id."_posts SET post_content = %s WHERE post_type='page' AND post_status = 'publish' AND post_name = %s";
$wpdb->query($wpdb->prepare($sql, $page->post_content, $page->post_name));
else:
$post_author = $current_user->ID;
$post_content = $page->post_content;
$post_title = $page->post_title;
$comment_status = 'closed';
$post_name = $page->post_name;
$post_type = 'page';
$insert = $wpdb->prepare( "( %d, %s, %s, %s, %s, %s)", $post_author, $post_content, $post_title, $comment_status, $post_name, $post_type );
$wpdb->query( "INSERT INTO ".$wpdb->base_prefix.$blog->blog_id."_posts ( post_author, post_content, post_title, comment_status, post_name, post_type ) VALUES " . $insert );
endif;
endforeach;
endforeach;