WordpressWordpress HooksWordpress Tricks

Post Author is Changed to Admin After his Post is Modified by Admin

Views

Hello to all, welcome to therichpost.com. In this post, I will tell you, how to stop Post Author is Changed to Admin After his Post is Modified by Admin.

If you are new in WordPress then you can check my old post related to WordPress and gain some information.

I was working in my WordPress project and that was multi vendor and when user creates the post from frontend and then admin will publish that pending post, then that post automatically goes to admin means means that post author now admin but I don’t want to this. So I made code to stop Post Author is Changed to Admin After his Post is Modified by Admin.

Here is the code for that and you need to add this code into your functions.php file:

<?php
function wpdocs_run_on_publish_only( $new_status, $old_status, $post ) {

   if( $old_status == 'pending' ) {
        update_post_meta( $post->ID, 'original_author', $post->post_author );

    }

    if( $new_status == 'publish' ) {
         $originalAuthor = get_post_meta( $post->ID, 'original_author' );

         if( !empty( $originalAuthor[0] ) && $originalAuthor[0] != $post->post_author ) {
             $postData = array(
                 'ID'           => $post->ID,
                 'post_author'  => $originalAuthor[0]
             );
             wp_update_post( $postData );    //May wish to check if this returns 0 for error-handling
         }
    }
}
add_action( 'transition_post_status', 'wpdocs_run_on_publish_only', 10, 3 );
?>

 

If you have any query related to this post then please let me know.

Jass

Thank you

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Positive SSL