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 ).

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;

3

The easiest way would be using get_post_field():

$post_author_id = get_post_field( 'post_author', $post_id );

For more details on this issue: have a look at this StackOverflow answer.

Leave a Comment