get post author id outside loop

I need to place in post edit dashboard metabox with post author e-mail (or other user meta fields). So it can be edited when admin reviews this post.

$meta_id = get_the_author_meta( 'user_email', $user_id );

$meta_box = array(
    'id' => 'my-meta-box',
    'title' => 'DANE FIRMY',
    'page' => 'post',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
        array(
            'name' => 'E-mail box',
            'id' => 'mail',
            'type' => 'text',
            'std' => $meta_id
        )
    )
);

This code works when $user_id is an integer (when I manually put there for example 4) but i want to dynamically get current author id ( $user_id ).

Read More

get_the_author_meta('user_mail') should work without specifying $user_id (codex says that :)) but code is in functions.php and outside the loop so it doesn’t work. I’m starting with WordPress and PHP so I don’t know what to do next.

Also tried this:

global $post;
$user_id=$post->post_author;

Related posts

3 comments

  1. You can use the following:

    /**
     * Gets the author of the specified post. Can also be used inside the loop
     * to get the ID of the author of the current post, by not passing a post ID.
     * Outside the loop you must pass a post ID.
     *
     * @param int $post_id ID of post
     * @return int ID of post author
    */
    function wpse119881_get_author( $post_id = 0 ){
         $post = get_post( $post_id );
         return $post->post_author;
    }
    
  2. add_action( 'edit_form_after_title', 'myprefix_edit_form_after_title' );
    function myprefix_edit_form_after_title() {
        global $post;
        $author_id=$post->post_author;
        $authord = get_the_author_meta( 'user_email', $author_id);
        echo $authord;
    }
    

    With this function I was able to display post author e-mail in post edit screen. Still don’t know how to make it work with custom meta field but I think Im closer now.

Comments are closed.