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

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

by therichpost
0 comments
wordpress

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

You may also like

Leave a Comment

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