Get Latest Contributors in WordPress
by c.bavota | Posted in Tutorials | 16 comments
If you have a WordPress blog with multiple contributors, you might want to display a list of which ones have been recently active. There isn’t a direct way to gather this information without querying the database directly but I found a simple alternative using core WP functions. I’m even going to show you how to cache the info using the WordPress Transients API.
First let’s create the function in our functions.php file. Be sure to place it between open PHP tags.
function get_latest_authors( $number_of_authors = 10 ) {
$args = array(
'orderby' => 'modified',
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => '-1'
);
$count = 1;
$recent_posts = wp_get_recent_posts( $args );
$latest_authors_array = array();
foreach( $recent_posts as $the_post ) {
if ( $count == $number_of_authors ) break;
if ( ! in_array( $the_post['post_author'], $latest_authors_array ) ) {
$latest_authors_array[] =$the_post['post_author'];
$count++;
}
}
$latest_authors = '<ul class="cdc-latest-authors">';
foreach( $latest_authors_array as $author_id ) {
$latest_authors .= '<li><a href="'.get_author_posts_url( $author_id ).'">'.get_the_author_meta( 'display_name', $author_id ).'</a></li>';
}
$latest_authors .= '</ul>';
return $latest_authors;
}
This function gathers all of your published posts, ordered by the last modified date. Then it loops through them gathering the authors. When it finds the number of authors you want, the looping stops and the list gets created and returned.
All it takes now is to echo out the returned data:
<?php echo get_latest_authors(); ?>
Since a function like this would be processed each time it’s called, we should take advantage of the WordPress Transients API to cache the returned data. That way it will only processes function according to when we set the transient to expire. All it takes is a little wraparound if statement:
function get_latest_authors( $number_of_authors = 10 ) {
if ( false === ( $latest_authors = get_transient( 'latest_authors' ) ) ) {
$args = array(
'orderby' => 'modified',
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => '-1'
);
$count = 1;
$recent_posts = wp_get_recent_posts( $args );
$latest_authors_array = array();
foreach( $recent_posts as $the_post ) {
if ( $count == $number_of_authors ) break;
if ( ! in_array( $the_post['post_author'], $latest_authors_array ) ) {
$latest_authors_array[] =$the_post['post_author'];
$count++;
}
}
$latest_authors = '<ul class="cdc-latest-authors">';
foreach( $latest_authors_array as $author_id ) {
$latest_authors .= '<li><a href="'.get_author_posts_url( $author_id ).'">'.get_the_author_meta( 'display_name', $author_id ).'</a></li>';
}
$latest_authors .= '</ul>';
set_transient( 'latest_authors', $latest_authors, 60 * 60 * 1 ); // one hour
}
return $latest_authors;
}
With the above modifications, our function will first check to see if a transient exists. If it does, it will return the stored data. If not, it will process the function and store the returned data in a transient which will expire after 1 hour (60 * 60 * 1). You can easily change when the transient will expire by modifying the last parameter in the set_transient() function.
Using transients to store returned data is a great way to speed up your site and reduce the number of processes required by your server for elements that might not change too frequently. That’s how the folks at WordPress usually do it.



Hello
What about getting all posts just for the last 10 authors !?
I think we can do a custom query like that :
“SELECT DISTINCT(post_author) FROM wp_posts ORDER BY ID DESC LIMIT 10″
What do you think about that ?
I think use
global $wpdb;
then use $wpdb->get_results(“/SQL in here/”);
And foreach is best!
Anything idea??
I believe if you replace
$latest_authors = ''; foreach( $latest_authors_array as $author_id ) { $latest_authors .= '<a href="'.get_author_posts_url( $author_id ).'">'.get_the_author_meta( 'display_name', $author_id ).'</a>'; } $latest_authors .= '';with
foreach( $latest_authors_array as $author_id ) { $auth_args = array( 'orderby' => 'modified', 'post_type' => 'post', 'post_status' => 'publish', 'numberposts' => '-1', 'author' => $author_id ); $posts = get_posts( $auth_args ); $latest_authors = ''; foreach( $posts as $post ) { $latest_authors .= '<a href="' . the_permalink(). '">' . the_title() . '</a>'; } $latest_authors .= '';I think that should give you all the posts for the last 10 authors.
The site stripped out some of the HTML on my above comment, you can view the full code here http://pastebin.com/mx8s5XFE
Can you some how modify the code to include the number of articles published by the author to show along with the name?
Example :
author1 (5 articles)
author2 (4 articles)
You can use the following:
Hi C.bavota can u please tell me how i can add this into my site? i mean i will have to add this code into widget of some file like functions.php or something like that? Awaiting your reply
thanks
sir i am still waiting for your reply
The function above would go into your functions.php file. If you wanted to use it in a widget then you would have to extend the WordPress Widget class. Read about that here: http://codex.wordpress.org/Widgets_API#Developing_Widgets
ok so when i will add this into my functions.php where it will appear?
It will appear wherever you place the
<?php echo get_latest_authors(); ?>Hello, Neat post. There’s an issue together with your web site in web explorer, may test this? IE still is the marketplace leader and a large component to people will omit your wonderful writing due to this problem.
IE is actually the third most used browser, after Firefox and Chrome. Check out the statistics here: http://www.w3schools.com/browsers/browsers_stats.asp