I realise that my problem in saving the slug, is that it causes an infinite loop.
Instead, I added
$my_post['post_name'] = '';
to
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_name'] = '';
$posttypes = array( 'page', 'post', 'portfolio' );
$currentposttype = get_post_type($post_id);