I am trying to create a plugin to copy a post to another site when a post is published in Worpdress Multisite environment using publish_post
and wp_insert_post
hook. The code is below
function copy_post_to_blog($post_id) {
$post = get_post($post_id, ARRAY_A); // get the original post
$post['ID'] = ''; // empty id field, to tell wordpress that this will be a new post
switch_to_blog(main_blog_id()); // switch to target blog
wp_insert_post($post); // insert the post
restore_current_blog(); // return to original blog
}
add_action('publish_post', 'copy_post_to_blog');
The code is working and inserting post data to the blog but the problem is that loop does’t stop inserting new post until I stop the browser loading execution myself. As it should stop after inserting post into database first time but it does’t and starts inserting post infinitely. Please Help me how to get ride of this problem.
I am thankful to you in advance.