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

?>

Posted August 11, 2009 by Alex Mansfield

48 responses to “WordPress Plugin: Latest Posts by Author”

  1. welzie says:

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

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

  3. Nikolas says:

    it doesn’t work. you suck

    • 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.

  4. shawn says:

    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)

  5. PG says:

    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

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

  7. PG says:

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

  8. Blogcastor says:

    Can you make this plugin WPMU compatible?

  9. 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/

  10. Antoine says:

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

  11. 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?

    • Ben says:

      I think what Antoine was referring to is that the list of related posts this plug in outputs don’t have the permalink /domain/article-title as the URL they have the domain.com/?p=1234.

      Is there a way to modify the plug in to output the permalink URL as opposed to the default?

    • Ben says:

      I think Antoine was referring to the fact that this plug in outputs the URLs as: domain.com/?p=1234 as opposed to domain.com/post-title/ Is this possible to accomplish using this plug in?

    • alexmansfield says:

      I changed line 24, so now it should be grabbing the permalink rather than using $post->guid. It turns out $post->guid isn’t recommended for use in this situation anyway. I’ve updated both my plugin in the WordPress repository and the code posted on this page. Thanks for pointing out the permalink issue.

  12. Serhat says:

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

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

  13. 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.

  14. Steve W says:

    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.

  15. Steve W says:

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

  16. Eric says:

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

  17. G01010 says:

    Hi there,

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

  18. G01010 says:

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

  19. Jason Paul says:

    So this is almost exactly what I’ve been looking for. Is there a way to show the Author’s Name as I’m using the shortcode multiple times in succession in the loop and I need to differentiate who the posts are from?

  20. Justin says:

    Plugin looks great and seems to be exactly what I’m looking for. But I’m having problems with the excerpt displaying, despite me setting ‘excerpt=”true”‘ in the shortcode I’ve put in my template. Any thoughts or suggestions?

  21. Justin says:

    Just an update…
    Figured out that my ‘post_excerpt’ field was blank on the posts I was testing with. So I added a little function to create an excerpt on the fly so that something shows up and a conditional to check whether or not to run it.

    [latest-posts-by-author.php]
    if($excerpt = ‘true’) {
    if($numpost->post_excerpt) {
    $html .= ”.$numpost->post_excerpt.”;
    } else {
    $html .= ”.string_limit_words($numpost->post_content, 60).”;
    }

    [functions.php]
    // create manual excerpt without post_excerpt
    add_filter(‘force_excerpt’, ‘string_limit_words’);
    function string_limit_words($full_content, $word_limit) {
    $exploded_content = explode(‘ ‘, $full_content);
    $excerpt = implode(‘ ‘, array_slice($exploded_content, 0, $word_limit));
    return $excerpt;
    }

    anyways, thought this could be helpful. thanks again for a great plugin!

  22. @Jason Paul

    You should be able to echo the $author wherever you’d like it to appear.

    @Justin

    You’re welcome! Thanks for sharing that piece of code.

  23. Jason Paul says:

    Thanks Alex…that may be beyond my skillset at this point…would that be something I do within your plugin code? ex: (that code didn’t give me the desired effect)

    (I haven’t studied php so It’s I have still to learn to to form these things). Anyway let me know. Thanks!

  24. Yes, that would be within the plugin code.

  25. Josh Johnson says:

    Hi , I had installed this plug in and it worked great, but I have recently moved the entire wordpress site to a different folder. Everything else transferred fine, but this plug in still shows links using the old folder. Is there an additional location where the URL needs to be updated?

    Thanks,
    Josh

  26. It appears that the “guid” column in the posts table needs to be updated with the new location. More info on that can be found here:

    http://www.hashbangcode.com/blog/wordpress-database-changes-when-moving-site-470.html

  27. Deron says:

    Hi there, I tried to use it but the result shows up like this,

    image: http://i51.tinypic.com/2ytv1xj.png

    may i know how to make them have line break?

    Thanks. lovely plugin you got here!

  28. dzu says:

    how can i show the related post together with the post thumbnail?

  29. dzu says:

    will this work to display in single page template?

  30. @dzu I wrote this plugin before WordPress added post thumbnail support, so it isn’t built in at the moment. You can find the code to work with post thumbnails in the WordPress documentation:

    http://codex.wordpress.org/Function_Reference/the_post_thumbnail

    …and yes, it should work in the single page template.

  31. @Deron Sorry for the delay. If you haven’t figured it out already, you should be able to increase the spacing by adding margin and/or padding to the “.latestbyauthor li” elements with CSS.

  32. Jack Rugile says:

    Hey, thanks for the great plugin! Everything is working well. I just had a quick question.

    I am concerned about performance for an author that has a large number of posts. The MySQL query seems to grab all of the author’s posts, then loop through until the iterator reaches the value of $show, then break the loop. To increase performance and only grab what you need from the database, can’t you add “LIMIT $show” to the end of the query (line20), and drop the for loop altogether? I don’t know if this is possible (I don’t work much with MySQL queries when it comes to WordPress, just wanted to throw it out there).

    Let me know your thoughts, and thanks again!

  33. Fazle Rabby says:

    That is a plugin for showing recent posts by author. But I need a plugin or code to show recent comments by author. Can anybody help me ?

Leave a Reply

Your email address will not be published. Required fields are marked *

  • PublishedAugust 11, 2009
  • UpdatedFebruary 16, 2012
  • Posted InPlugins, Wordpress
  • DifficultyAdvanced
  • Tested With2.8 - 3.3