Last active
June 5, 2020 13:35
-
-
Save donini/a9c1e7ef6bc74dbb09bdc824bb04232b to your computer and use it in GitHub Desktop.
Move Posts Between WordPress Sites
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 | |
add_filter( 'bulk_actions-edit-post', 'move_posts_bulk_multisite_actions' ); | |
function move_posts_bulk_multisite_actions( $bulk_array ) { | |
if( $sites = get_sites( array( | |
'site__not_in' => get_current_blog_id(), // excluding current blog | |
'number' => 50, | |
))) { | |
foreach( $sites as $site ) { | |
$bulk_array['move_to_'.$site->blog_id] = 'Move to "' .$site->blogname . '"'; | |
} | |
} | |
return $bulk_array; | |
} | |
add_filter( 'handle_bulk_actions-edit-post', 'move_posts_bulk_action_multisite_handler', 10, 3 ); | |
function move_posts_bulk_action_multisite_handler( $redirect, $doaction, $object_ids ) { | |
// we need query args to display correct admin notices | |
$redirect = remove_query_arg( array( '_posts_moved', '_blogid' ), $redirect ); | |
// our actions begin with "move_to_", so let's check if it is a target action | |
if( strpos( $doaction, "move_to_" ) === 0 ) { | |
$blog_id = str_replace( "move_to_", "", $doaction ); | |
foreach ( $object_ids as $post_id ) { | |
// get the original post object as an array | |
$post = get_post( $post_id, ARRAY_A ); | |
// if you need to apply terms (more info below the code) | |
$post_terms = wp_get_object_terms($post_id, 'category', array('fields' => 'slugs')); | |
// get all the post meta | |
$data = get_post_custom($post_id); | |
// empty ID field, to tell WordPress to create a new post, not update an existing one | |
$post['ID'] = ''; | |
switch_to_blog( $blog_id ); | |
// insert the post | |
$inserted_post_id = wp_insert_post($post); // insert the post | |
// update post terms | |
wp_set_object_terms($inserted_post_id, $post_terms, 'category', false); | |
// add post meta | |
foreach ( $data as $key => $values) { | |
// if you do not want weird redirects | |
if( $key == '_wp_old_slug' ) { | |
continue; | |
} | |
foreach ($values as $value) { | |
add_post_meta( $inserted_post_id, $key, $value ); | |
} | |
} | |
restore_current_blog(); | |
// if you want to copy posts, comment this line | |
wp_delete_post( $post_id ); | |
} | |
$redirect = add_query_arg( array( | |
'_posts_moved' => count( $object_ids ), | |
'_blogid' => $blog_id | |
), $redirect ); | |
} | |
return $redirect; | |
} | |
add_action( 'admin_notices', 'move_posts_bulk_multisite_notices' ); | |
function move_posts_bulk_multisite_notices() { | |
if( ! empty( $_REQUEST['_posts_moved'] ) ) { | |
// because I want to add blog names to notices | |
$blog = get_blog_details( $_REQUEST['_blogid'] ); | |
// depending on ho much posts were changed, make the message different | |
printf( '<div id="message" class="updated notice is-dismissible"><p>' . | |
_n( '%d post has been moved to "%s".', '%d posts have been moved to "%s".', intval( $_REQUEST['_posts_moved'] ) | |
) . '</p></div>', intval( $_REQUEST['_posts_moved'] ), $blog->blogname ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment