15

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?

38

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.

9

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

<?php
global $post;
$author_id=$post->post_author;
?>

Then it is possible to us the_author_meta:

<?php
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
3

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.

<?php
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.

<?php
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.

<?php
// make sure you're at the beginning.
rewind_posts();

// start the loop
the_post();

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

// back to normal
rewind_posts();
2

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.

0

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:

<?php    

$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()) {              
    $com_query->the_post();
        $author_name= get_the_author_meta('user_nicename',  $feat_posts[$count]->post_author);
        $count++;
}
-2

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.