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:




global $post;


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


$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?


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.


Here’s how to obtain and get the author ID outside the WordPress loop:

global $post;

Then it is possible to us the_author_meta:

the_author_meta( 'user_nicename', $author_id );
  • This works great if you have access to the post ID. You can also use get_the_author_meta('user_nicename', $author_id ) if you don't want to output the value straight away – Andrew M Dec 21 '16 at 9:44

Depends on where you are. If you're on a singular page (eg. only showing a single {{Insert Post Type Here}}), you could use get_queried_object, which will fetch the post object.

if (is_singular()) {
    $author_id = get_queried_object()->post_author;
    $address = get_the_author_meta('user_email', $author_id);

If you're anywhere else, you could use the global $wp_query object, and check its $posts property. This should work on singular pages as well.

global $wp_query;
if (!empty($wp_query->posts)) {
    $author_id = $wp_query->posts[0]->post_author;
    $address = get_the_author_meta('user_email', $author_id);

You can also just "false start" the loop and rewind it to grab the author ID. This will no incur any additional database hits or the like. WordPress fetches all posts at once (at the time of writing). rewind_posts just resets the current post (the global $post) object to the beginning of the array. The downside is that this may cause the loop_start action to fire way earlier than you want it to -- not a huge deal, just something to be aware of.

// make sure you're at the beginning.

// start the loop

// get what you need
$address = get_the_author_meta('user_email');

// back to normal

This looks like it works outside of the loop, maybe this will help.

    $thelogin = get_query_var('author_name');
    $theauthor = get_userdatabylogin($thelogin);

You could also manually set ID of post and grab this way:

global $wp_query;
$thePostID = $wp_query->post->ID;
$postdata = get_post($thePostID, ARRAY_A);
$authorID = $postdata['post_author'];

Change ID out to post id manually for out of the loop access.

Not great solutions, but hopefully it helps.


I had the same issue here when trying to make a widget that displayed featured posts with author information.

I used some of the hint's from @chrisguitarguy 2nd tip.

My code looked like this:


$count = 0;
$query_args = array(
      'posts_per_page' => 5,
$com_query = new WP_Query( $query_args );

$feat_posts = $com_query->posts; // array, so we can access each post based on position

while ($com_query->have_posts()) {              
        $author_name= get_the_author_meta('user_nicename',  $feat_posts[$count]->post_author);

Why don't you use the_author_meta

<p>The email address for user id 25 is <?php the_author_meta('user_email',25); ?></p>

This can be used within the loop

  • Thanks, but the problem is I'm outside the loop and can't fix that. When you're outside the loop, the second argument ($author_id) needs to be provided. – Marce Castro Sep 18 '12 at 22:16
  • Bump! Any ideas? It's driving me crazy :-/ – Marce Castro Sep 21 '12 at 18:41
  • 3
    outside the loop - please note the question. – Christine Cooper Dec 26 '12 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.