Building on the venerable Recent Posts plugin by Ron and Andrea, I have created an extended version that offers a lot more user control over output, including gravatar support. The basic features are:
- excludes posts on main blog (blog ID = 1)
- excludes first posts (Hello, world) on user blogs (post ID = 1)
- option to show gravatar support (24px). Gravatar links to posts by user on their blog.
- option to show post excerpt. User can specify excerpt length with extra argument. Option to capitalize 1st five words of excerpt for readability.
- option to show post author name
- option to show post date
- option to show post comment count
- all dispay options can be selectively toggled on or off using a single bitmask parameter, permitting very flexible and customizable usage (256 possible configurations!)
- numerous other display and formatting options can be easily edited in source code using global vars
The argument list:
- $how_many: how many recent posts are being displayed
- $how_long: time frame to choose recent posts from (in days)
- $optmask: bitmask for various display options (default: 255)
- 1; // gravatar
- 2; // date
- 4; // author name
- 8; // comment count
- 16; // blog name
- 32; // post name
- 64; // post excerpt
- 128; // excerpt capitalization
- $exc_size: size of excerpt in words (default: 30)
- $begin_wrap: start html code (default: <li class=”ahp_recent-posts”>)
- $end_wrap: end html code to adapt to different themes (default: </li>)
To use the bitmask option, simply add the numeric codes for the display options you want together. For example, suppose you only want gravatar, post name, and date – then the bitmask would be 1+2+32 = 35. Using a bitmask in this way, you can turn on or off any combination of display options above, using only a single function argument.
The latest version (0.6) of this plugin is at WPMUDEV with a more complete description, installation instructions, screenshot, and sample function call. You can see also see the plugin in action at Talk Islam, under the sidebar section titled “Recent Journals”.


{ 108 comments… read them below or add one }
← Previous Comments
Have you figured out how to display most recent posts, regardless of the owner?
So in other words, even if the last 5 were from the same blog, still list them all…..
This is much needed – thanks for any help.
Aziz – Great plugin! I am using it for a wpmu installation at work, and it works great. I was wondering if you had a way to paginate the results, or if you were working on adding that functionality at all. My predecessor had a custom script that returned the 300 latest blog posts, but only showed 30 at a time on a page.
Keep up the awesome work.
Hi,
Is there a way to show the scrips outside the WPMU folder? How can I use the script on my mainpage (non WP page)?
Thanks guys!
Great plugin – thanks for all your hardwork.
Unfortunately, i’m getting a slight error.
If I include blog name it includes and unwanted / for example: joe blogg/’s
I’ve tried edited the plugin but I cannot see what would be causing this. Any ideas anyone?
please someone explain where exactly should I put this code?
Am I doing something wrong? below you can see the part of my home.php file
WordPress µ
This is a WordPress Mu powered site.
Is it possible to limit the posts displayed to one blog and one category within that blog?
I don’t see the enclosing in my widget in my theme. I would like ot be able to class that UL as well. What am I misising?
Oops, post 57 should read “I don’t see the enclosing <ul> in my theme…”
Hey Aziz, can you contact me? I widgetized version 0.32 of this plugin a long time ago, and even added some of the changes you did. However, your version of the plugin looks much much nicer, and I would like to merge my code with yours: I would like to send you a widgetized version of your 0.61 that you can post as an update on wpmudev.org. If you’re not interested in continuing development, maybe you can add me to your wpmudev project or let me manage it. Otherwise, please let me know how to contact you so I can give you my code.
Running into a slight problem.
Ive uploaded the entire folder to mu-plugins
Ive uploaded both files to mu-plugins
No matter how I do it, I keep getting this
ahp_recent_posts plugin is missing, the widget will not be enabled until the plugin is installed
So what did I do wrong.
Thank you for plugin
Here is some modded up SQL for including only specific blogs in the query.
Around line 13, just replace this line which excludes the main blog
delete = AND blog_id != ‘1′
With this
AND blog_id IN (’2′,’3′,’4′)
And use the Blog ID for the blogs you want to include, all others not listed will be left out of the plugin’s results.
Useful if you want to feature/drive traffic to certain blogs.
Make that “around line 131″ in the above.
Also, in case it wasn’t clear, I’m talking about modifying the file “ahp_recent_posts” version 0.6.1, though it may work for others.
Hello, is there possible to show image on every recent post rather than just an excerpt?
or maybe you can merge this plugin with advance excerpt plugin (http://wordpress.org/extend/plugins/advanced-excerpt/)
Thank you
Hi there,
thanks for this nice plugin. Would be possible to show recent posts only of specified blog ID? I mean show recent posts of e.g. blog ID 1 in sidebar and blog ID 2 in footer. Thanks.
Before anything, thank you very much for this great plugin!!
Now this is my problem:
I have a blog (id=28) that has many authors, but I would like to show a specific avatar in the excerpt instead of the authors avatar, so that it’s always the same image. (Actually the excerpts from that blog display each author’s avatar, that’s what I want to change).
Please help me!
Thank you very much for your time!
I’ve put your code on my site in three locations – not very pretty, but the basic code anyway.
But instead of any content from the blogs, even though it’s picking up the blogger and the blog address link correctly, the content itself is either the � symbol or a blank box. Clearly I’ve done something wrong. Any idea what?
hi!
Well, I put this code:
but text don’t go at the right side of the gravatar…
how could I do?
Thanks
why does code not appear?
Hi Aziz,
I’m having a little bit of trouble with your plugin.
The posts are supposed to be sorted from latest to oldest; but I get a few that get misplaced in the order.
Also I’m not getting the latest post for one of the blogs; I’m getting the second one instead.
Any idea why?
I have the same issue with one of my blogs showing the second newest post instead of the most recent. Does anyone know why this happens or how to fix it?
Okay I’ve looked into the matter.
For the “not getting the latest post” bug, in your $thispost SQL query… you should use ORDER BY $blogPostsTable.post_date instead of $blogPostsTable.ID.
Also about the weird, blog order bug I was experiencing… I guess $wpdb->blogs looks at the last updated activity in the admin area and not just posts.
But I guess there isn’t an easier way to quickly identify which blog has the latest post, right?
Ray,
I figured out the issue with the second newest post showing instead of the newest… on mine. The ID numbers had somehow gotten out of sync and the second newest post had a higher ID number than the newest post. The mySQL query in this plugin sorts by ID number. So the highest (in this case, the second newest post) ID number showed.
You can do one of two things to correct this. Go into PHPmyAdmin and renumber the IDs to have the newest post be the highest number. (Pain in the butt and runs the risk of having this happen again.)
Or change the mySQL query to order by post_date instead of ID.
I hope this helps you.
Now, if I could just figure out how to get more than one post per blog to show….
Darrin,
I already listed the post_date solution in the comment right above yours.
Getting more than one post is easy; change “DESC limit 0,1″ to “DESC limit 0,x”. Where “x” is the number you want to show.
Ray,
Sorry I missed that.
I tried changing to “DESC limit 0,3″, but still just get one post from each blog. I know the mySQL query returns 3 results. I’ve tested it. I must be missing something else.
Darrin
Hey Darrin,
I thought changing the limit would allow you to have more posts from the same blog. But I didn’t check it out myself! hehe.
I get the problem now… Aziz hardcodes $thispost so only the first entry is outputted. eg. $thispost[0].
What you’d need to do is do a foreach loop after the if($thispost) conditional.
eg.
if(thispost) {
foreach($thispost as $thePost) {
...
Then change all remaining intances of $thispost[0] to $thePost. Make sure you close the foreach loop!
That should allow you to output more than one post per blog!
Let me know if that works!
Hmmm…. I had tried that before and it didn’t work. I changed it to:
foreach ($thispost as $thePost) :
… do stuff …
endforeach;
I got an error saying “Cannot use object of type stdClass as array…” Something about it pulling an object as result. I’m not highly proficient at PHP yet, so I haven’t worked out the fix for it.
Darrin
Darrin, sorry for not replying sooner. Theres a backlog of issues I need to address. Rest assured I will be looking at this soon.
great plug-in
but i can’t seem to install it properly
will it work in my wordpress 2.6 version?
As pointed out above, the first search gets each blog by the last update activity, which doesn’t necessarily equate to the most recent post.
I was wondering, could you take the query results for each blog and put them into an array, then sort that array based on the post date and output the values in that method? This way, it wouldn’t matter which blog was updated last, the newest post would be first.
I’m going to look into it myself and I will post back if I get it to work.
Okay, this is how you do it.
Get blog list
Create Array
Create Counter
Loop thru blogs
Get posts (change query to have post_date as first column)
use
$wpdb->get_row($sql, ARRAY_A);where “$sql” is the post queryinsert results into array
if($thispost) {
$postArray[$i] = $wpdb->get_row($sql, ARRAY_A);
$i++;
}
sort array
array_multisort($postArray, SORT_DESC);
loop through array as before to post the code
The only issue right now is the blog link, but I’ll probably figure that out soon and then I’ll post a complete example.
Okay, I posted my fix. Make sure that you notice that I changed the formatting of how the posts are displayed. This formatting might not work for your needs.
Sitewide Recent Posts – hack
Okay, after finishing that little hack, I decided to do a little bit more.
I wanted to show more than one post per site, so I added the ability to set the limit per site. This doesn’t override the total number of posts seen, but it shows more per site. It still sorts them all by date.
I did a write up and posted the code.
AHP Sitewide Recent Posts – hack 2.0
This is a great plugin. What would I put in my CSS to change the font on this??
HI,
what a good plugin, i have implemented it on my wpmu, but is there someone here who can tell me how to makes older post on the bottom of ahp plugin ? so there will be an older post link that show all of the older posting( like 1/2 day before ) . Any help or suggestion is appreciated
thanks
I figured out out to strip out wordpress’s caption shortcode and make sure that a excerpt is displayed when whitespace precedes the actual text in the excerpt.
Whitespace became an issue for me when stripping out html and captions from the beginning of the except because the first character encountered was a blank space, causing the script to think that there was no excerpt for a blog post.
Somewhere around line 242 insert a preg_replace to remove the caption shortcode and a trim() to remove the extra whitespace:
// get post content and truncate to (numwords) words
$thiscontent = strip_tags( $thispost[0]->post_content );
$thiscontent = preg_replace(“/\[caption.*\[\/caption\]/”, “”, $thiscontent);
$thiscontent = trim($thiscontent);
preg_match(“/([\S]+\s*){0,$exc_size}/”, $thiscontent, $regs);
Instead of:
// get post content and truncate to (numwords) words
$thiscontent = strip_tags( $thispost[0]->post_content );
preg_match(“/([\S]+\s*){0,$exc_size}/”, $thiscontent, $regs);
Thanks for this great plugin, but I didn’t make it work…
When I wanna active it, there’s an error:
Cannot redeclare ahp_recent_posts() in ../wp-content/plugins/1104069568_ahp_recent_posts_0.6.1.php on line 90
ya, it’s plugins, ‘coz when I put it in mu-plugins, the WPMU even can’t find it~
Please help me, thank you!
alright, it fixed.
I uploaded the php file both to plugins/ and mu-plugins/, then the function is defined twice….
sorry …
Is there any code/ or adjustment to make this paginated? Would be awesome to have the ability to show more pages of results.
Anyone has done this before?
How can I get all the features ahp Recent post offers like: gravatar, date, author name,comment count, blog name, post name, post excerpt, excerpt capitalization to work on my regular Recent Post widgets. I don’t want it from all blogs just single blogs?
Hello, how the addition of plug-in page displays only one article?
Sorry, about only one article . I think I understand..Thank you.
en..
Will be able to add comments link do?
I made another update to the code I modified.
I changed the display part so that the different parts of the display turn off based on the option code
AHP Sitewide Recent Posts – hack 2.2ish
Hi Aziz
I have had this plugin installed for awhile now and it was working fine. After a break from working on the site over Christmas, I have came back and found that no matter what I try in editing the arguments, the plugin will only list 1 recent post regardless of the number I list in the code. I am currently using the simplest code to show you what I mean with the latest version of wpmu . . .
Any thoughts or suggestions would be great.
Please disregard my question. I figured it out by adding a new post to a different blog and it is now showing that post as well.
Thanks for your great plugin – the only trouble I have: how can I disable the html-stripping. In the 0.6.1 version I can’t find the strip_tags …
thanks werner
I have the exact same question as werner, how to disable the html stripping.
Thanks!!!
Really like the plugin a lot! I did change a few things.
Order of most recent blog post issue.
It sorts the list of site by when it was last updated on the master blogs table. Problem with this is a lot of my users update pages on their blogs and not posts as often. So it showed those sites as recently updated. I added some SQL to the query and sorted by Post Date of each blog to get the most recent posts.
Odd Coding showing in posts
Stripped out more of the wordpress only captions and such using php. Worked good for my site.
Look for strip_tags to get rid of the HTML stripping.
@chris: sadly there is no strip_tags function in the version 0.6.1 …
← Previous Comments