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: https://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 in ,

48 thoughts on “WordPress Plugin: Latest Posts by Author”

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

  1. 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)

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

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

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

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

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

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

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

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

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

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

  8. 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!

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

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

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

  12. 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!

    1. After a little research, I decide to rewrite my plugin using the WP_Query class rather than the $wpdb object. Rather than running a SQL query directly, it uses the standard WordPress query methods. I believe the “posts-per-page” setting in the new plugin code should restrict the amount of data pulled from the database. Let me know if you have any issues with the new version. Also, I credited you in the changelog: http://wordpress.org/extend/plugins/latest-posts-by-author/changelog/

Leave a Comment

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