Wordpress Plugin: Latest Posts by Author
Tuesday, August 11th, 2009

Update: The plugin has been updated with the option of including post excerpts in version 0.4. See: http://wordpress.org/extend/plugins/latest-posts-by-author/
I just posted my first plugin to wordpress.org. It creates an unordered list of the most recent posts by a given author. It can be called both from within a post using a shortcode or from within a theme file. Check out Latest Posts by Author at wordpress.org for more details.
To demonstrate just how easy it is to create a plugin for Wordpress, I’m posting the plugin code below.
<?php
/*
Plugin Name: Latest Posts by Author
Plugin URI: http://wordpress.org/#
Description: Displays a list of recent posts by the specified author
Author: Alex Mansfield
Version: 0.2
Author URI: http://alexmansfield.com/
*/
function latest_posts_by_author($array) {
extract(shortcode_atts(array('author' => 'admin', 'show' => 5), $array));
global $wpdb;
$table = $wpdb->prefix . 'users';
$result = $wpdb->get_results('SELECT ID FROM '.$table.' WHERE user_login = "'.$author.'"');
$id = $result[0]->ID;
$table = $wpdb->prefix . 'posts';
$result = $wpdb->get_results('SELECT * FROM '.$table.' WHERE post_author = '.$id.' AND post_status = "publish" AND post_type = "post" ORDER BY post_date DESC');
$i = 0;
$html = '<ul>';
foreach ($result as $numpost) {
$html .= '<li><a href="'.$numpost->guid.'">'.$numpost->post_title.'</a></li>';
$i++;
if($i == $show){
break;
}
}
$html .= '</ul>';
return $html;
}
add_shortcode('latestbyauthor', 'latest_posts_by_author');
?>