0

I'm trying to display a table format of the top authors listed by the most recent posts. It should display the author, its avatar, the date, and the title of the post (with a link).

I have it listing the top authors along with the avatar. But it won't display the title of the post or the correct date.

Here's what I have so far:

$uc=array();
$blogusers = get_users_of_blog();
if ($blogusers) {
foreach ($blogusers as $bloguser) {
$userpost = get_posts('showposts=1&author='.$bloguser->user_id);
$uc[$bloguser->user_id] = '';
if ($userpost) {
  $uc[$bloguser->user_id]=$userpost[0]->post_date;
}
}
arsort($uc);
$i = 0;
foreach ($uc as $key => $value) {

$user = get_userdata($key);
$post_id= $user->postID;
$post_title= $user->post_title;
$post_date= $user->post_date;
$post_count = get_usernumposts($user->ID);
if ($post_count && $i < 10) {
  $author_posts_url = get_author_posts_url($key);      
  ?>
  <table width="280" border="0" cellspacing="0" cellpadding="5">
                  <tr> 
                      <td width="15%"> <img src="<?php bloginfo ('template_directory'); ?>/images/authors/<?php print $user->user_login; ?>.jpg" onerror= "this.src='<?php bloginfo ('template_directory'); ?>/images/authors/default.jpg'" width="45" height="50"></td>
                      <td width="85%" align="left">
                          <div class="sidebar_post"><?php printf ("<a href=\"index.php?p=%s\">%s</a></li>", $post_id,$post_title)?><br></div>
                          <div class="sidebar_author">By <?php print $user->display_name;?><br></div>
                          <div class="sidebar_postdate">Posted <?php the_time(get_option('date_format'));?></div>
                      </td>
                  </tr>
                    <tr>
                      <td colspan="2"><hr></td>
                  </tr>
                </table>
 <?php    }
 $i++;
}
}

2 Answers 2

1

Do a WP_User_Query and then go through the results:

$wp_user_search = new WP_User_Query( array( 
    'role' => 'author', 
    'fields' => 'all_with_meta' 
) );
$editors = $wp_user_search->get_results();
echo '<pre>';
print_r( $editors );
echo '</pre>';
-2

Update: To get around this, I changed it up. It's probably not an ideal fix but it gave me what I needed.

            // Selects from tables wp_users and wp_posts, selecting published posts and ordering by its post_date, limited to 10
            $querystr = "SELECT * from wp_users, wp_posts WHERE wp_users.ID = wp_posts.post_author AND wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish' ORDER BY wp_posts.post_date DESC LIMIT 10";

              $news=$wpdb->get_results($querystr, OBJECT);

              foreach($news as $np)
              {
                $authorID = $np->authorID;
                $author_nickname = $np->user_login;
                $author_displayName = $np->display_name;
                $author_niceName = $np->user_nicename;
                $post_id= $np->postID;
                $post_title= $np->post_title;
                $post_date= $np->post_date;
1
  • That's a) an unescaped and unsafe query open for injections and b) not portable as you don't use the proper prefixes.
    – kaiser
    Commented Feb 11, 2012 at 15:31

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.