Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This displays authors by date of their latest post. I'd like to:

  1. add formatting around each author
  2. list additional properties (avatar, latest post title and excerpt) <-- I have the bits for this, but not sure where to insert them here.

    <ul>
    <?php
    //List of users sorted descending by date of latest post written by user
    $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);
      foreach ($uc as $key => $value) {
        $user = get_userdata($key);
        $post_count = get_usernumposts($user->ID);
        if ($post_count) {
          $author_posts_url = get_author_posts_url($key);
          echo '<li><a href="' . $author_posts_url . '">' . $user->user_firstname . ' ' .         $user->user_lastname . '</a> (' . $post_count . ') </li>';
        }
      }
    }
    ?>
    

share|improve this question
    
What formatting and where? and... where do you want these properties. Maybe show us a text-based-wire-frame structure of how you want this to work otherwise its a guessing game. –  userabuser Oct 11 '12 at 4:44
    
I have the formating (and the extra info bits I mentioned) laid out here: pastebin.com/zxcyM8Zn –  Phantasmix Oct 11 '12 at 10:04
    
By the way, that pastebin code is exactly what I need with one exception: users are sorted by ID. If there's a way use that code instead and simply add the sorting - even better! Thank you –  Phantasmix Oct 11 '12 at 10:17
    
Ehm, so what is your question currently - how to add info or how to sort? –  Rarst Oct 11 '12 at 18:48
    
It's both, actually. Right now I have formatting and all the data in place, but there's no sorting. So it must be both 1) sorted by last post, AND 2) formatted. –  Phantasmix Oct 11 '12 at 19:21
show 3 more comments

1 Answer 1

up vote 1 down vote accepted

I'm finding it a little hard to follow based on your comments, however if you want to add a <div> around each other then change your second foreach statement to;

  foreach ($uc as $key => $value) {
    $user = get_userdata($key);
    $post_count = get_usernumposts($user->ID);
    if ($post_count) {
      $author_posts_url = get_author_posts_url($key);
      echo '<div class="class-name-here">';
      echo '<li><a href="' . $author_posts_url . '">' . $user->user_firstname . ' ' .         $user->user_lastname . '</a> (' . $post_count . ') </li>';
      echo '</div>';
    }
  }

That's based on the assumption you want to keep the <li> element, if not, just replace that with the <div>

Update (with regards to your comment)

$args=array(
  'author' => $user->ID,
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => 1,
  'caller_get_posts'=> 1,
  'orderby' => 'author'   //add the orderby parameter and sort by author
);

As I mentioned in my comment attached to your question above, you can try adding the orderby parameter and then assign author as its value which should sort via ID or alternatively if that does not work then replace author with $user->ID

Update 2

After running some tests on your original snippet, removing deprecated functions and adding the orderby=date parameter to your initial foreach statement (although it neither effects with or without) I am still able to retrieve a list of authors, showing their last post, then ordering all results returned by the most recent date. So if,

  • User A posts 12/10/2012
  • User B posts 15/10/2012
  • User C posts 11/10/2012

The order of results returned is,

  • User B
  • User A
  • User C

$uc=array();
$blogusers = get_users();
if ($blogusers) {
  foreach ($blogusers as $bloguser) {
    $userpost = get_posts('showposts=1&author='.$bloguser->ID.'&orderby=date');
    $uc[$bloguser->ID] = '';
    if ($userpost) {
       $uc[$bloguser->ID]=$userpost[0]->post_date;
    }
  }
  arsort($uc);
  foreach ($uc as $key => $value) {
    $user = get_userdata($key);
    $post_count = count_user_posts($user->ID);
    if ($post_count) {
      $author_posts_url = get_author_posts_url($key);
        echo '<li><a href="' . $author_posts_url . '">'.$user->user_firstname . ' '  $user->user_lastname . '</a> (' . $post_count . ') </li>'
    }
  }
}

Although the above snippet could be rewritten more efficiently, it does work for me and so it should for you with relation to what you're trying to achieve.

share|improve this answer
    
Hi @userabuser well... that was a waste of time - just tested the original code, and it doesn't sort authors by latest post. Sorry for the confusion, too. I have a nearly-perfect working version (Pastebin) and then found this bit (above). Couldn't settle on one, until now. Please let me know if you have any ideas as to how to sort the Pastebin code. Thank you! –  Phantasmix Oct 15 '12 at 5:40
    
I added 'orderby' => 'author', it didn't change anything. It's the default sort order according to the internet :) –  Phantasmix Oct 15 '12 at 5:48
    
Btw, I include @userabuser at the beginning, but it gets erased. –  Phantasmix Oct 15 '12 at 5:49
    
Yes, it does sort by user ID. That's correct. I want to sort authors by latest post, so that the most recently active one is always at the top. –  Phantasmix Oct 15 '12 at 5:50
1  
@Phantasmix I should up-vote you for your confusion factor :) That aside, I ran some tests and made slight modifications to the original snippet provided and it works for me. So, it should work for you too. –  userabuser Oct 15 '12 at 7:26
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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