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.