So pretty much.. Is there a PHP command I could use

So pretty much.. Is there a PHP command I could use to clone my posts from one blog to another?

I want to clone a post from one blog to another on the same multisite network. Nothing fancy... I just want to run a cron job to do this instead of using the interface... But would love to rely on the plugin rather than pulling the post apart myself.

Thanks!

  • Bojan Radonic
    • Head of Support

    Hey there @Chippy Media Inc.,

    Hope you're well today and thank you for your question.

    Using Cloner for that is possible but it will require adding some some custom code. Please add this code to your theme functions.php:

    function wpmudev_copy_just_one_post() {
        switch_to_blog( $destination_blog_id );
        $copier = copier_get_copier( 'posts', $source_blog_id, array(), $user_id );
    
        // Avoid deleting the rest of the posts in the destination blog
        add_filter( 'wpmudev_copier_get_delete_posts_args', 'wpmudev_set_delete_posts_args' );
    
        // Sets the parameters to get the source posts to copy
        add_filter( 'wpmudev_copier_get_source_posts_args', 'wpmudev_set_source_posts_args' );
    
        $copier->copy();
    
        restore_current_blog();
    }
    
    function wpmudev_set_delete_posts_args( $args ) {
        // This post type does not exist so the function should return an empty posts array
        $args = array( 'post_type' => 'fake_post_type');
        return $args;
    }
    
    function wpmudev_set_source_posts_args( $args ) {
        $args = array(
            'posts_per_page' => 1,
            'p' => $source_post_id
        );
    
        return $args;
    }

    Make sure to replace $user_id for the author post ID or 0 if you don't mind and replace $source_blog_id and $source_post_id for the blog/post IDs that you want to copy from.

    As an alternative you could try to use Multisite Content Copier to copy posts from one blog to another.

    Hope this helps :slight_smile:

    Best regards,
    Bojan

  • Chippy
    • WPMU DEV Initiate

    Is there any way to make it copy categories if it can't find the name/slug of the category already?

    It just places the posts as uncategorized and I need it to be intelligent to use or create the same categories.

    Thanks.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.