How to get Author ID outside the loop

I can’t get the post author ID outside the loop to make the get_the_author_meta work. So far I’ve tried different approaches:

1.

$author_id=$post->post_author;

2.

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

3.

$post_tmp = get_post($post_id);
$author_id = $post_tmp->post_author;

4.

$author_id = $posts[0]->post_author;

I need the author ID to pass it on to:

$address = get_the_author_meta('user_email', $author_id);

Any suggestions?

8

The simplest and most straightforward way to get the post author ID outside the loop, if you know the post ID, is to use the WordPress core function get_post_field().

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

If you do not yet know the post ID of the page you are on, then since WP 3.1 the easiest thing to do is use the get_queried_object_id()(look for it in the list of Methods) function which works even outside the loop.

$post_id = get_queried_object_id();

If these do not work for you then please give a more detailed explanation of where you are trying to run your code and we can see if we can help further.

Leave a Comment