WordPress.org

Ready to get started?Download WordPress

Forums

Get author meta data at 0 posts? (13 posts)

  1. wzshop
    Member
    Posted 1 year ago #

    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?

  2. keesiemeijer
    moderator
    Posted 1 year ago #

  3. wzshop
    Member
    Posted 1 year ago #

    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!

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    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?

  5. wzshop
    Member
    Posted 1 year ago #

    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

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    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.

  7. wzshop
    Member
    Posted 1 year ago #

    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.

  8. wzshop
    Member
    Posted 1 year ago #

    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

  9. keesiemeijer
    moderator
    Posted 1 year ago #

    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

  10. wzshop
    Member
    Posted 1 year ago #

    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

  11. keesiemeijer
    moderator
    Posted 1 year ago #

    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?

  12. wzshop
    Member
    Posted 1 year ago #

    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.

  13. keesiemeijer
    moderator
    Posted 1 year ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic