• We wanted to be able to sort the author list with the most recent poster at the top. Maybe you’d like to add this functionality to the main code base.

    This is a diff based on version 2.1.

    diff -br authors/authors.php authors/authors-patched.php
    24a25,43
    >     function widget_authors_add_last_post_date(&$author) {
    >    	 $args = array(
    >    		 'numberposts'    => 1,
    >    		 'author'    => $author->ID,
    >    		 'post_status'    => 'publish',
    >    		 'orderby'    => 'date',
    >    		 'order'   	 => 'DESC'
    >    	 );
    >    	 $last_posts = get_posts($args);
    >    	 if (count($last_posts) > 0) {
    >    		 $last_post_date = $last_posts[0]->post_date;
    >    		 $author->last_post_date = $last_post_date;
    >    	 } else {
    >    		 // Can't find any posts for this author, so set to the
    >    		 // first second in the epoch.
    >    		 $author->last_post_date = "1970-01-01 00:00:01";
    >    	 }
    >     }
    >
    34a54,64
    >     function widget_authors_sort_by_last_post($a, $b) {
    >    	 $a_date = strtotime($a->last_post_date);
    >    	 $b_date = strtotime($b->last_post_date);
    >
    >    	 if ($a_date == $b_date) {
    >    		 return 0;
    >    	 }
    >    	 // We use greater than here to sort in descending order
    >    	 return ($a_date > $b_date) ? -1 : 1;
    >     }
    >
    36c66
    <    	 if ('first_name' != $orderby && 'last_name' != $orderby) {
    ---
    >    	 if ('first_name' != $orderby && 'last_name' != $orderby && 'last_post' != $orderby) {
    39a70
    >    	 array_walk($authors, widget_authors_add_last_post_date);
    46a78,80
    >    	 case 'last_post':
    >    		 usort($authors, widget_authors_sort_by_last_post);
    >    		 break;
    471a506
    >    		 <label for="authors-order-<?php echo $number; ?>-lastpost"><input type="radio" class="radio" id="authors-order-<?php echo $number; ?>-lastpost" name="widget-authors[<?php echo $number; ?>][order]" value="last_post"<?php echo 'last_post' == $order || '' == $order ? ' checked="checked"' : '' ?> />&nbsp;<?php _e('Last post','authors') ?></label>

    http://wordpress.org/extend/plugins/authors/

  • The topic ‘A patch for "sort authors by most recent post"’ is closed to new replies.