How to get WordPress current post author ID and other details

wordpressYesterday while coding a function to show different adsense code for different author, I needed to find the author ID of that post to show the relevant google adsense code with it. The issue was solved by the wordpress builtin function get_the_author_meta.

The official reference page for get_the_author_meta function for wordpress says that it has to be used within the loop. Well let me make it clear here that this doesn’t mean that whole code has to be there. You can place the code in functions.php and call it within the loop. I used it entirely within the functions.php and added the adsense code before the content.

The get_the_author_meta function gives you various details of the author of particular post including his id, email, username and so on.

However, it don’t publish it directly, to echo or print the author meta you have to use the_author_meta function and it will echo it directly.

The function works like this:

<?php $user_email = get_the_author_meta('user_email'); ?>

You can use ID, nickname, first_name and other parameters too. For details check the get_the_author_meta functions page on WP Reference

Leave a Reply

Your email address will not be published.