Insert new post for each user infinite loop error

With that function in functions.php it is going to run on every page load and every time it runs it is going to insert a post for every user in your database. It isn’t an infinite loop. You are just running it all the time.

You need to run that one time and one time only to populate your DB with posts existing users. If you have low traffic on your blog you can just paste it in, load the page, and immediately remove it. If you have much traffic you will need a different strategy.

If you want to insert a post for new users, you need to hook a similar function into the user registration system. Something like…

function create_user_post($user_id) {
    $user = get_userdata($user_id);
    $my_post = array(
    'post_title'    => $user->display_name,
    'post_content'  => 'Bla bla bla.',
    'post_status'   => 'publish',
    'post_author'   => $user->ID,
    'post_type'     => 'custom'
    );

    wp_insert_post( $my_post );
}
add_action('user_register','create_user_post');

I just made that up off the top of my head, but I think it will work. You are just complicated enough with this that it might take some implementing and then debugging. 🙂

For reference: get_userdata, user_register