I have an odd problem with the author list. I am using the below code in my sidebar to pull in the gravatar and link to author's profile. However, the links it's actually generating are wrong. It just generates links to the page that it's on. So when you're on the blog page, all the author links are www.mysite.com/blog/. Any idea why this could be happening??

    $order = 'user_nicename';
    $user_ids = $wpdb->get_col("SELECT ID FROM $wpdb->users WHERE ID <> 1 ORDER BY $order"); // query users
    foreach($user_ids as $user_id) : // start authors' profile "loop"
    $user = get_userdata($user_id);
    <li class="clearfix"><?php echo get_avatar( $user->ID, '80' ) . '<a href="' . $user->user_url . '">' . $user->display_name . '</a>'; ?></li>
    endforeach; // end of authors' profile 'loop'


  • $user->user_url will not point to the user's profile on your site (as I think you think it does). It's the link the author has provided by editing his profile. If he hasn't updated his profile there will be no URL in the database and browsers interpret <a href="">afkjl</a> as a link to the page that it's currently on.
    – user5028
    Commented Oct 22, 2012 at 16:01
  • oh, thanks. is there any solution that can fix this to point to the author's url on my site?
    – jamie
    Commented Oct 22, 2012 at 16:18
  • 1
    I wrote it as an answer so that other's could find it. BTW if you are just interested in getting all user objects, you may want to use get_users instead of the wpdb class.
    – user5028
    Commented Oct 22, 2012 at 16:28

2 Answers 2


To generate a link to the author's page/archive on your website, use get_author_posts_url with the author's user ID.

In your case:

<li class="clearfix"><?php echo get_avatar( $user->ID, '80' ) . '<a href="' . get_author_posts_url( $user->ID ) . '">' . $user->display_name . '</a>'; ?></li>

On the one hand, there's the get_users() function Arguments @Codex that wraps up the user query class and should be used instead of a direct query.

On the other hand - if you're forced (as in: "someone is pointing a gun on my hand") - then you should prepare() your query properly:

$user_ids = $wpdb->get_col( $wpdb->prepare( 
     "SELECT ID FROM $wpdb->users WHERE ID <> 1 ORDER BY %s"
) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.