We are developing WordPress with multiple site. We need to share some posts to more than one site. We need to save post in more than one sites with a single click.

I have searched in Google, but I can’t get any tutorial for that.

2 Answers
2

You can use the function switch_to_blog() for this

$other_id = 1234 // the id of the other blog to save the post to
switch_to_blog($other_id);
$my_post = array(
  'post_title'    => $post_title,
  'post_content'  => $post_content,
  'post_status'   => 'publish',
  'post_author'   => $post_author,
  );

// Insert the post into the database
wp_insert_post( $my_post );

restore_current_blog();

There is a pitfall if you execute this code on the save_post hook, because wp_insert_post also calls save_post and you end up in an infinite loop. This post on Stack Overflow gives a solution for that.

Leave a Reply

Your email address will not be published. Required fields are marked *