1

I wanted to change the author when a post change from draft to publish. I have $_GET['auth_id'] variable in post edit screen like this ...wp-admin/post.php?post=53&action=edit&auth_id=5. I tried save_post hook to change the post author like below

function change_pos_auth($post_id){
        if ( ! wp_is_post_revision( $post_id ) ){

    // unhook this function so it doesn't loop infinitely
    remove_action('save_post','change_pos_auth');

   if ( isset($_GET['auth_id']) ) {
        $args = array('ID'=>$post_id,'post_author'=>$_GET['auth_id']);
    // update the post, which calls save_post again
    wp_update_post( $args );
   }

    // re-hook this function
    add_action('save_post','change_pos_auth');

}
}

This doesn't work out may be this might not have $_GET variables. I tried the get the current user to make them as the author

function change_pos_auth($post_id){
        if ( ! wp_is_post_revision( $post_id ) ){

    // unhook this function so it doesn't loop infinitely
    remove_action('save_post','change_pos_auth');
   if ( $last_user = get_post_meta( $post_id, '_edit_last', true) ) {
        $args = array('ID'=>$post_id,'post_author'=>$last_user);
    // update the post, which calls save_post again
    wp_update_post( $args );
   }

    // re-hook this function
    add_action('save_post','change_pos_auth');

}
}

Above also doesn't work. The main idea is that, it is a question & answer type. the author who first answer the question in admin becomes the author of the post. Question will be asked by admin by default.So i'm trying to change the author on save when it gets published. If the post is published already am not allowing other authors to the edit screen. I'll redirect to another page and say someone answered it already.

Any Help?

1

1 Answer 1

1

From the code you have posted there, it doesn't look like, at any time, you're are hooking the function on to save_post outside of your function.

function change_pos_auth($post_id){
    if ( ! wp_is_post_revision( $post_id ) ){

        // unhook this function so it doesn't loop infinitely
        remove_action('save_post','change_pos_auth');

        if ( isset($_GET['auth_id']) ) {
             $args = array('ID'=>$post_id,'post_author'=>$_GET['auth_id']);
             // update the post, which calls save_post again
             wp_update_post( $args );
        }

       // re-hook this function
       add_action('save_post','change_pos_auth');

    }
}
add_action('save_post', 'change_pos_auth');

The rest of the code looks ok providing $_GET['auth_id'] is populated.

NB: Don't forget to sanitize that value before you put it into the database.

3
  • Hi, thanks. When i save the post author is not changing to the one as i logged in. Any Idea why it is not changing the the post author. Also if i try to delete some post with this code enabled. I get error as Error in moving to Trash. Any idea? @Steven Jones
    – rram
    Commented May 1, 2014 at 18:19
  • The reason why you might get the error is because the user you are logged in doesn't have the capabilities to delete the post. Also you need to populate $_GET['auth_id'] with the ID of the person that's logged in - how you do that is up to you. Commented May 1, 2014 at 21:07
  • Hi steven. thanks for the delete explanation. $_GET['auth_id'] is not available on save post. I check this by checking the if(isset($_GET['auth_id'])){ //write in file as available}else{//write in file as not available} and writing whether it is present are not in a text file. It is not available.
    – rram
    Commented May 3, 2014 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.