WordPress.org

Support

Support » How-To and Troubleshooting » Get author meta data at 0 posts?

Get author meta data at 0 posts?

  • Hi,
    Is it possible to retrieve the authors (author.php) meta data when the author has published 0 posts? What code to use? I only can get it to work
    i.e. retrieve e-mail with: get_the_author_meta( 'user_email' ) when the user also published a post.

    Any ideas?

Viewing 12 replies - 1 through 12 (of 12 total)
  • Moderator keesiemeijer

    @keesiemeijer

    Ok thanks, was thing the wrong way i guess..

    Anyway so i have for instance this code:

    <?php $user_info = get_userdata($user->ID);
          echo 'email: ' . $user_info->user_email . "\n";
          echo 'User level: ' . $user_info->user_level . "\n";
          echo 'User ID: ' . $user_info->ID . "\n";
    ?>

    Why is this not returning anything? Has to do with the $user->ID part.. i know..

    Thanks!

    Moderator keesiemeijer

    @keesiemeijer

    Do you want to show the info of a currenty logged in user or just a specific user.
    Where do you want to show this?

    Hey, Thanks for your reply.
    I would like to show the info at (author.php) /author/author_name
    So not the current logged in user.

    But since sometimes an author has not published anything yet, the get get_the_author_meta will not work (i guess).. So i need a different solution..

    Hope you can help me out.
    Thanks.
    Robbert

    Moderator keesiemeijer

    @keesiemeijer

    On a author template you can use get_query_var(‘author’) to get the author ID.

    <?php
    $current_author =  get_query_var('author');
    if($current_author) {
    	$user_info = get_userdata($current_author);
    	if($user_info) {
               echo 'email: ' . $user_info->user_email . "\n";
               echo 'User level: ' . $user_info->user_level . "\n";
               echo 'User ID: ' . $user_info->ID . "\n";
            }
    }
    ?>

    Also, if you’re going to show the email from authors on the front end of your site ask permission first.

    Ok thanks, that is great.
    Is there an easy plugin to ask for permission? Or should i custom script it and save it as user metadata?

    Thanks.

    One more question

    How to implement the get_author_meta, such as <?php echo get_the_author_meta('facebook'); ?> into the above mentioned method?

    Thanks

    Moderator keesiemeijer

    @keesiemeijer

    Did you try it with the user_id:

    <?php
    $current_author =  get_query_var('author');
    if($current_author) {
      echo get_the_author_meta( 'facebook', $current_author );
    }
    ?>

    http://codex.wordpress.org/Function_Reference/get_the_author_meta

    Ok thanks a lot for helping me out on this. That works!
    I am trying to understand these codes. I know something goes wrong with getting/using the user_id. Please allow me to ask you some questions:

    -This is outside the loop, so the user_id needs to be specified right?
    -The user_id is used in the $current_author variable, in echo get_the_author_meta( 'facebook', $current_author ); right?
    -The user_id is obtained in the code $current_author = get_query_var('author'); right?
    -Now, how should i find out that getting the user_id is accomplished through $current_author = get_query_var('author');? I have read the codex, but can’t find/see this solution as such. Am I missing something?

    Thanks

    Moderator keesiemeijer

    @keesiemeijer

    This is outside the loop, so the user_id needs to be specified right?

    On author archives the user_id is specified (inside and outside the loop). As before you can get it by using get_query_var('author');
    You test if the user_id is found by:

    $current_author =  get_query_var('author');
    // check if the user_id is found
    if($current_author) {
      // the user_id is found
    } else {
      // no user_id found
    }

    Do you want to use this on other templates?

    Ok, so the query you specified get_query_var('author'); is just something to know, or is there a way for me to find/found that out myself?
    Thanks again, just trying to understand.

    Moderator keesiemeijer

    @keesiemeijer

    If you would go to an author page with the permalink structure set to default it would look something like this yoursite.com/?author=23. As you can see the user_id is available in the url. Based on that WordPress knows what posts to show and what variables to populate the global query object with.
    http://codex.wordpress.org/WordPress_Query_Vars

    If you’re not on a live site and you want to see the global query object (what query vars are available) try putting this just above the loop:

    <?php
    global $wp_query;
    echo '<pre>';
    print_r($wp_query);
    echo '</pre>';
    ?>

    http://codex.wordpress.org/Function_Reference/WP_Query#Parameters

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Get author meta data at 0 posts?’ is closed to new replies.
Skip to toolbar