Wordpress Plugin: Latest Posts by Author

latest-by-author

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');

?>

Category: Wordpress | Tags: Plugins, Wordpress Follow comments with RSS 2.0

25 Responses to “Wordpress Plugin: Latest Posts by Author”

  1. welzie | August 18, 2009 at 9:52 pm

    Thanks for the plugin. I made one slight change. I added post_status = "publish" because I often have drafts saved. Thx again.

  2. alexmansfield | August 19, 2009 at 9:50 am

    Thanks for sharing your improvement! I’ll try to get that added to the plugin later this week.

  3. alexmansfield | August 20, 2009 at 11:06 pm

    Thanks again welzie! I updated the plugin to include your fix and I credited you in the changelog.

  4. Nikolas | August 21, 2009 at 10:10 am

    it doesn’t work. you suck

  5. alexmansfield | August 21, 2009 at 10:48 am

    It’s working on this site (see the “Recent” section on the sidebar), and it appears that it worked for welzie also. Maybe you could give me a few more details, Nikolas, and I could help you get it working on your site as well.

  6. shawn | October 24, 2009 at 2:04 pm

    I’m curious how I would go about modifying the code to output the latest posts by author for the ‘author’ of the single page being viewed.

    To be more clear:
    I am viewing a single article page written by user ‘xyz’ I would like to show the most recent posts by that author below the post, after the loop.

    User xyz would need to be determined somehow instead of hardcoded.

    any ideas?

    (not using as a plugin, but added to theme functions file, and inserted into template directly)

  7. alexmansfield | October 30, 2009 at 12:34 am

    I would probably use the get_the_author() function (http://codex.wordpress.org/Function_Reference/get_the_author) and assign the result to a variable which I would then use in the Latest Posts by Author code. I haven’t actually tried this, but that’s the direction I think I would be heading.

  8. PG | January 26, 2010 at 9:13 pm

    Thanks for the plugin.
    I have a modification request (if possible). Is there anyway to add an option to display the summary of the post contents not just a link to the post (e.g Post Title and Post Extract)?
    I am interested in creating an author page and want to display the last say 5 posts with a summary of each post.
    Thanks

  9. alexmansfield | January 27, 2010 at 1:03 pm

    That’s a good idea, PG! I’ll try to look into it later this week. Thanks for the suggestion.

  10. PG | February 12, 2010 at 6:51 pm

    Great, thanks Alex.
    I look forward to an update.

  11. Blogcastor | February 28, 2010 at 2:42 am

    Can you make this plugin WPMU compatible?

  12. alexmansfield | February 28, 2010 at 12:27 pm

    The next version of Wordpress will be merged with WPMU, so I plan on waiting for that to happen instead of modifying the plugin right before the merge.

  13. alexmansfield | April 9, 2010 at 12:39 pm

    PG, I’ve finally updated the plugin to allow for the post excerpt to be displayed. http://wordpress.org/extend/plugins/latest-posts-by-author/

  14. Antoine | April 13, 2010 at 7:20 am

    Hi,
    Is there a way to use /%postname%/ permalink structure?
    Thanks

  15. alexmansfield | April 13, 2010 at 4:49 pm

    Antoine, I’m not sure what you’re referring to. This plugin doesn’t deal with permalinks. Can you please give a few more details?

  16. Serhat | April 27, 2010 at 5:36 am

    How do i change “username” to “author”?

    This code i want to show “authors” post. Can you help me? Thank you.

  17. alexmansfield | April 28, 2010 at 8:59 am

    I couldn’t find “username” in the script, so I assume that you are speaking of “user_login” at the beginning of the script. It is simply looking for posts by the author with that username. If that’s not what you were trying to accomplish, please post a few more details and I’ll see what I can do for you.

  18. Steve W | May 28, 2010 at 2:00 am

    In reply to shaun – if you add the code inside the loop you can use this syntax:

    So it’s picking up the author name from the loop.

  19. Steve W | May 28, 2010 at 2:01 am

    do_shortcode('[latestbyauthor author="'. the_author().'" show="3"] ');

  20. alexmansfield | May 28, 2010 at 4:23 pm

    Thanks Steve!

  21. Eric | July 23, 2010 at 10:47 am

    I can only say the following: Your plugin is great because it is written well, easy to install and quite funtional!

  22. alexmansfield | July 23, 2010 at 1:16 pm

    Thanks Eric! I’m glad you found it helpful.

  23. G01010 | August 20, 2010 at 6:14 am

    Hi there,

    Any tips on how to modify this to display the latest post for more than one author?

  24. G01010 | August 20, 2010 at 6:30 am

    Actually, don’t worry. I used this:

    $numposts = $wpdb->get_results('SELECT ID, post_author, post_title FROM '.$wpdb->posts.' WHERE post_status=\'publish\' AND post_type=\'post\' GROUP BY ID ORDER BY ID DESC');

  25. alexmansfield | August 20, 2010 at 9:09 am

    Thanks for posting your solution, G01010!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>