Get Latest Contributors in WordPress

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 = '';
	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:


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 = '';
		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.

Share this:

Email
Facebook
Twitter
Pinterest
Pocket

Premium Themes for WordPress

Looking for an easy-to-use Premium Theme for WordPress? Check out Themes by bavotasan.com and have your site up and running in no time.

Use this WordPress website builder to build powerful websites in no time for your or your clients.

WordPress Hosting

WP Engine – designed from the ground-up to support and enhance the workflow of web designers.

Bluehost – providing quality web hosting solutions since 1996.

About the author

Picture of Luke Perrie

Luke Perrie