Categories

Friday, April 19, 2024
#919814419350 therichposts@gmail.com
WordpressWordpress HooksWordpress Tricks

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

Wordpress trick to check term id has parent term or child term

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

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

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