Support » Plugins » wp_insert_post infinite loop

  • Resolved ruocks

    (@ruocks)


    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

    (@ruocks)

    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.