WordPress Plugin: Latest Posts by Author
Update: The plugin has gone through a number of revisions since I first published this post. See: http://wordpress.org/extend/plugins/latest-posts-by-author/ for the latest versions. The code posted below is from version 0.6.
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.6
Author URI: http://alexmansfield.com/
*/
function latest_posts_by_author($array) {
extract(shortcode_atts(array('author' => 'admin', 'show' => 5, 'excerpt' => 'false'), $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="'.get_permalink($numpost->ID).'">'.$numpost->post_title.'</a>';
if($excerpt == 'true'){
$html .= '<p>'.$numpost->post_excerpt.'</p>';
}
$html .= '</li>';
$i++;
if($i == $show){
break;
}
}
$html .= '</ul>';
return $html;
}
add_shortcode('latestbyauthor', 'latest_posts_by_author');
?>