Copy a page between site in multisite network

Question

Currently, I’m trying to add a function code to copy posts and pages between sites in my multisite network. the code looks like this:

add_filter( 'bulk_actions-edit-page', 'misha_my_bulk_multisite_actions_page' );
 
function misha_my_bulk_multisite_actions_page( $bulk_array ) {
 
    if( $sites = get_sites( array(
        // 'site__in' => array( 1,2,3 )
        '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( 'bulk_actions-edit-post', 'misha_my_bulk_multisite_actions' );
 
function misha_my_bulk_multisite_actions( $bulk_array ) {
 
    if( $sites = get_sites( array(
        // 'site__in' => array( 1,2,3 )
        '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', 'misha_bulk_action_multisite_handler', 10, 3 );
 
function misha_bulk_action_multisite_handler( $redirect, $doaction, $object_ids ) {
 
    // we need query args to display correct admin notices
    $redirect = remove_query_arg( array( 'misha_posts_moved', 'misha_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();
 
        }
 
 
        $redirect = add_query_arg( array(
            'misha_posts_moved' => count( $object_ids ),
            'misha_blogid' => $blog_id
        ), $redirect );
 
    }
 
 
    return $redirect;
 
}

That code is working, but I just can copy my post not my page. So do you guys know, what code should I need to add to copy the page between site in my multisite network?

Thank you,

0
Lily Ferliansyah 2 years 2020-12-17T04:10:27-05:00 0 Answers 7 views 0

Leave an answer

Browse
Browse