在 WordPress 中的帖子更新时更改帖子作者

waz*_*day 3 php wordpress

当作者在仪表板中的帖子上单击“更新”时,如何让帖子作者自动更改为任何作者?

我试图使用此代码在更新帖子时触发某些内容,但没有任何反应。有任何想法吗?

add_action( 'publish_post', 'changeAuthor' );

function changeAuthor($post_id){
    echo "hello";
}
Run Code Online (Sandbox Code Playgroud)

Bar*_*art 5

这可能是要调用的函数...代码未经测试。

add_action('save_post', 'functiontocall');

functiontocall () {
    if ( ! wp_is_post_revision( $post_id ) ){

        $my_post = array(
            'ID'            => $post_id,
            'post_author'   => get_current_user_id(),
        );


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

        // update the post, which calls save_post again
        wp_update_post( $my_post );

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

    }

}
Run Code Online (Sandbox Code Playgroud)