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'); ?>
Thanks for the plugin. I made one slight change. I added
post_status = "publish"
because I often have drafts saved. Thx again.Thanks for sharing your improvement! I’ll try to get that added to the plugin later this week.
Thanks again welzie! I updated the plugin to include your fix and I credited you in the changelog.
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.
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)
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.
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
That’s a good idea, PG! I’ll try to look into it later this week. Thanks for the suggestion.
Great, thanks Alex.
I look forward to an update.
Can you make this plugin WPMU compatible?
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.
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/
Hi,
Is there a way to use /%postname%/ permalink structure?
Thanks
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?
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?
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?
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.
How do i change “username” to “author”?
This code i want to show “authors” post. Can you help me? Thank you.
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.
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.
do_shortcode('[latestbyauthor author="'. the_author().'" show="3"] ');
Thanks Steve!
I can only say the following: Your plugin is great because it is written well, easy to install and quite funtional!
Thanks Eric! I’m glad you found it helpful.
Hi there,
Any tips on how to modify this to display the latest post for more than one author?
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');
Thanks for posting your solution, G01010!
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?
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?
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!
@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.
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!
Yes, that would be within the plugin code.
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
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
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!
how can i show the related post together with the post thumbnail?
will this work to display in single page template?
@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.
@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.
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!
That’s a really good point. I’ll have to look into that and get back to you.
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/
The new version looks great. Thank you for the credit, and good luck with future plugins!
Thanks, and you’re welcome!
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 ?
Feel free to use the contact form on this website to give me a few more details about what you’re trying to do. Then I’ll see if I can find something that will work for you.
https://alexmansfield.com/contact