The MU forums have moved to WordPress.org

Show user nickname by ID (3 posts)

  1. Akashic
    Member
    Posted 16 years ago #

    Hi,

    I wanted to display users by nickname knowing only their ID.

    As to be more specific: I created separate table and inserted some date (along with user ID). Obviously I want to list the table with user nickname rather than ID.
    Up until now I made up with that kind of (working) thing:

    $photos = mysql_query("SELECT * FROM photos")
    while($list = mysql_fetch_array( $photos ))
    {
      $user = $wpdb->get_col("SELECT meta_value
    	FROM $wpdb->usermeta WHERE
    	meta_key = 'nickname' AND user_id = '$list[ID]'
    	ORDER BY user_id DESC");	
    
    	echo $user[0];
    }

    I'm just worrying that the loop will gonna call to the database too often, am I right?
    Let's say that if I will have 10000 elements in the database, than in one hour 100 users will open the page listing (calling the loop) several elements. This will generate a lot of queries.
    Is there any other, simpler way of getting user nickname by ID or maybe a way to optimize the code?

  2. andrea_r
    Moderator
    Posted 16 years ago #

    http://codex.wordpress.org/Author_Templates#Custom_Author_Information

    Not exactly sure if that's all of what you want or need, but <?php echo $curauth->nickname; ?> grabs the nickname.

  3. Akashic
    Member
    Posted 16 years ago #

    Thanks for reply.

    That's not exactly what I need.
    In the database there are many users' ID and I want to show their nicknames (not only the current logged user nor author of the post).
    Anyway, after studying the MU source, I made up with this fully working code:

    function get_nickname_by_id( $auth_id ) {
    	$authordata = get_userdata( $auth_id );
    	return $authordata->nickname;
    }

    Cheers,
    A.

About this Topic