Created
December 23, 2015 00:13
-
-
Save trishasalas/056ea1c42fc32ebec63b to your computer and use it in GitHub Desktop.
This will migrate sites from a multisite into the main blog.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/********************************* | |
|||| MAGIC.SCRIPT |||| | |
|||| XX |||| | |
|||| ----------------- |||| | |
|||| for migrating |||| | |
|||| multisite posts |||| | |
|||| into a single site |||| | |
**********************************/ | |
/* | |
* For my purposes this file needed to reside outside | |
* of the wp-content directory. I didn't import everything | |
* at once because I like to keep an eye on things to avert | |
* potential disasters. | |
*/ | |
require('wp-blog-header.php'); | |
global $wpdb; | |
$args = array( | |
'network_id' => $wpdb->siteid, | |
'public' => null, | |
'archived' => null, | |
'mature' => null, | |
'spam' => 0, | |
'deleted' => null, | |
'limit' => 100, | |
'offset' => 0, | |
); | |
$my_sites = wp_get_sites( $args ); | |
foreach( $my_sites as $my_site ) { | |
$tc_blog_id = $my_site['blog_id']; | |
switch_to_blog( $tc_blog_id ); | |
echo '<h2>The current blog id is ' . get_current_blog_id() . '</h2>'; | |
$posts = get_posts( | |
array( | |
'posts_per_page' => 500, | |
'post_status' => 'publish', | |
'post_type' => 'post' | |
) | |
); | |
echo '<h3>There are ' . count( $posts ) . ' posts in this blog.</h3>'; | |
//var_dump( $posts ); | |
restore_current_blog(); | |
foreach ( $posts as $post ) { | |
//echo '<p>' . $post->post_title . ' ' . '-- ' . $post->post_author . '</p>'; | |
$args = array( | |
'post_title' => $post->post_title, | |
'post_content' => $post->post_content, | |
'post_author' => $post->post_author, | |
'post_date' => $post->post_date, | |
'post_date_gmt' => $post->post_date_gmt, | |
'post_name' => $post->post_name, | |
'post_status' => $post->post_status, | |
'ping_status' => $post->post_status, | |
'to_ping' => $post->to_ping, | |
'pinged' => $post->pinged, | |
'post_modified' => $post->post_modified, | |
'post_modified_gmt' => $post->post_modified_gmt, | |
'post_parent' => $post->post_parent, | |
'guid' => $post->guid, | |
'post_type' => $post->post_type, | |
'post_mime_type' => $post->post_mime_type, | |
'comment_count' => $post->comment_count, | |
); | |
wp_insert_post( $args ); | |
} | |
} | |
wp_cache_flush(); | |
//var_dump($my_sites); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment