Title: wp_insert_post infinite loop
Last modified: August 20, 2016

---

# wp_insert_post infinite loop

 *  Resolved [ruocks](https://wordpress.org/support/users/ruocks/)
 * (@ruocks)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/wp_insert_post-infinite-loop/)
 * I’m trying to create a plugin in multisite network that allows me, when save 
   a post, to save it in another blog of my choice.
 * _[ Moderator Note: Please post code or markup snippets between backticks or use
   the code button. ]_
 *     ```
       function test($post_id){
           $post = get_post($post_id);
           $post = get_object_vars($post);
           unset($post[ID]);
           $blog_id = 4;
           switch_to_blog($blog_id);
           wp_insert_post($post, true);
           restore_current_blog();
       }
   
       add_action( 'save_post', 'test', 1, 1 );
       ```
   
 * But this causes an infinite loop. Does anyone know where I’m wrong?
 * Thanks a lot

Viewing 1 replies (of 1 total)

 *  Thread Starter [ruocks](https://wordpress.org/support/users/ruocks/)
 * (@ruocks)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/wp_insert_post-infinite-loop/#post-3077970)
 * I resolved my problem
 *     ```
       function test($post_id){
           $post = get_post($post_id);
           $post = get_object_vars($post);
           unset($post[ID]);
           $blog_id = 4;
           switch_to_blog($blog_id);
           remove_action('save_post', 'test');
           wp_insert_post($post, true);
           add_action('save_post', 'test');
           restore_current_blog();
       }
   
       add_action( 'save_post', 'test')
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘wp_insert_post infinite loop’ is closed to new replies.

 * 1 reply
 * 1 participant
 * Last reply from: [ruocks](https://wordpress.org/support/users/ruocks/)
 * Last activity: [13 years, 8 months ago](https://wordpress.org/support/topic/wp_insert_post-infinite-loop/#post-3077970)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
