Support » Plugins » Hacks » Author Not Updating in save_post hook

  • 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_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);
    			$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;
Viewing 2 replies - 1 through 2 (of 2 total)
  • Okay a little additional self diagnosis on this bad boy yielded that if I dump the get_post() of the post ID after I update it that the author HAS been updated.

    $set = wp_insert_post($post_update, true);
    var_dump($set);  // yup that's the id
    var_dump(get_post($set)); // ooh yay the author has the correct ID~!

    Though my current author still overrides it if I let all the other hooks complete, ie. remove the die();

    Documentation says that this hook runs after the data is inserted into the DB <; but there’s clearly still core WP actions happening after the hook to overwrite my updated author to the author I’m currently logged in as.

    I know there are no other plugins or actions occurring that would override this. Only 3 plugins installed, 2 of which I wrote and the other I’ve already checked for any author references. Nothing in the parent theme’s functions or child theme’s functions either.

    I think wp_insert_post requires an array. That will say:

    $post_update = array(
    ‘post_author’ => $authorID


    $post_update = array();
    $post_update[‘post_author’] = $authorID;

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Author Not Updating in save_post hook’ is closed to new replies.