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();