Hello All,
I have a blog that requires a new author for a lot of my new posts. I built myself a quick little meta box to be able to create users on-the-fly within the custom post type pages.
The problem I'm running into is that the function that is called in the save_posts hook creates the new author but doesn't assign it to the post. Here's the hook and function to see if there's anything y'all can think of that I might be doing wrong here.
add_action('save_post', 'cf_save_metabox_author');
function cf_save_metabox_author(){
global $post;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post->ID;
$author_username = isset($_POST['author_username'])?$_POST['author_username']:false;
$author_email = isset($_POST['author_email'])?$_POST['author_email']:false;
$author_displayname = isset($_POST['author_displayname'])?$_POST['author_displayname']:false;
$author_website = isset($_POST['author_website'])?$_POST['author_website']:false;
$savable = ($author_username && $author_email && $author_displayname && $author_website);
if(!empty($post) && isset($post->ID) && $savable)
{
$author = array(
'user_login'=>$author_username,
'user_email' =>sprintf($author_email, $author_username),
'user_url' =>$author_website,
'nickname' => $author_displayname,
'first_name' => $author_displayname,
'role' =>'contributor',
'display_name' =>$author_displayname,
'user_pass'=>wp_generate_password( 12, true )
);
$authorID = wp_insert_user($author);
if(is_numeric($authorID))
{
$post_update = $post;
$post_update->post_author = $authorID; // This should work on it's own...but doesn't
$_POST['post_author_override'] = $authorID; // this is what the custom post type interface is passing...
$post_update->post_author_override = $authorID; // just trying everything!
return wp_insert_post($post_update, true);
}
}
return false;
}