Skip to content

Instantly share code, notes, and snippets.

@ultimagriever
Last active December 2, 2016 20:17
Show Gist options
  • Save ultimagriever/ed26cc0f532529fff292f5b0393ce2de to your computer and use it in GitHub Desktop.
Save ultimagriever/ed26cc0f532529fff292f5b0393ce2de to your computer and use it in GitHub Desktop.
Export WordPress Multisite to Single Site
<?php
define("GLOBAL_URL", "insert your wordpress master url here, e.g. master domain");
// Export master blog without comments - handled by Facebook plugin
exec('wp export --url="' . GLOBAL_URL . '" --skip_comments --filename_format="{site}.{date}.xml"');
$sites = shell_exec("wp site list --url=\"" . GLOBAL_URL . "\" --field=url");
$sites = explode(PHP_EOL, $sites);
foreach ($sites as $site) {
$url = str_replace("http://", "", $site);
$name = str_replace("/", "", substr($url, strlen(GLOBAL_URL)));
if (strlen($name) < 1) {
continue;
}
$id = str_replace(PHP_EOL, "", shell_exec('wp term create category Blogs --slug="blogs" --porcelain --url="' . $url . '"'));
$cat = str_replace(PHP_EOL, "", shell_exec('wp term create category "$(wp option get blogname --url=' . $url . ')" --slug=' . $name . ' --parent=' . $id . ' --porcelain --url="' . $url . '"'));
echo "Category " . $cat;
$posts = shell_exec('wp post list --url="' . $url . '" --field=ID');
$posts = explode("\n", $posts);
foreach ($posts as $post) {
$post = str_replace(PHP_EOL, "", $post);
exec('wp post term add ' . $post . ' category $(wp term get category ' . $cat . ' --url="' . $url . '" --field=slug) --url="' . $url . '"');
}
// Export blog without comments - those are being handled by Facebook
exec('wp export --url="' . $url . '" --filename_format="{site}.{date}.xml" --skip_comments');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment