Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Copies a post (as well as its associated meta data) from one wordpress blog to another on the same network.
<?php
function copy_post_to_blog($post_id, $target_blog_id) {
$post = get_post($post_id, ARRAY_A); // get the original post
$meta = get_post_meta($post_id);
$post['ID'] = ''; // empty id field, to tell wordpress that this will be a new post
switch_to_blog($target_blog_id); // switch to target blog
$inserted_post_id = wp_insert_post($post); // insert the post
foreach($meta as $key=>$value) {
update_post_meta($inserted_post_id,$key,$value[0]);
}
restore_current_blog(); // return to original blog
}
?>
@Lukapotamus

This comment has been minimized.

Copy link

commented Jan 2, 2014

Hey Murdaugh,

Happy New Year!

This is a great piece of code! Do you know how I would go about ignoring just one piece of the metadata but copying everything else?

Kind Regards

Luke Pope

@msherstobitow

This comment has been minimized.

Copy link

commented Feb 6, 2016

@Lukapotamus, you can modify foreach-loop like this, add at the beginning of the loop body:

if ( 'some_value' === $key ) { continue; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.