Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 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.

share|improve this answer
    
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 May 1 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. –  Steven Jones May 1 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 May 3 at 20:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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