How to Remove Draft Posts from your Recent Posts

A few weeks back, I redesigned my plain looking footer to include Popular Posts, Categories, Most Recent Posts and also all the other websites & blogs where my article has been featured(All those bloggers have been kind enough to send me a logo to reuse on my site).

I didnt realize this at first, but one of my readers pointed out that some of my posts are not available. Since then I have been looking all around my site to see what may have been leading to a 404 page on my site, and until yesterday I couldnt figure out that my “Recent Posts” section was also displaying the posts that I have in a “draft” stage for future publishing.

This was really embarrassing to let my readers think I have published a particular article when I havent yet completed the same. So I started looking around to fix this and found it was pretty simple to filter out the non-published ones from appearing here.

If you are facing a similar problem, you can use the code below to rectify the issue.

$number_recents_posts = 5;
$args=array(‘numberposts’ => $number_recents_posts,’post_status’=>’publish’);
$recent_posts = wp_get_recent_posts($args); foreach( $recent_posts as $recent ){ echo ‘<li><a href=”‘ . get_permalink($recent[“ID”]) . ‘” title=”Look ‘.$recent[“post_title”].'” >’ . $recent[“post_title”].'</a> </li> ‘; }

The following two tabs change content below.
Praveen Rajarao is an Entrepreneur and in his spare time blogs on his website –http://www.dailymorningcoffee.com and http://www.pbgeeks.com. His topics range from blogging to technology to affiliate programs and making money online and how-to guides. Daily Morning Coffee is also accepting Guest Posts from Professional Bloggers at this time, take a look at “Write For DMC” page for more details on the same.

Latest posts by Praveen Rajarao (see all)

6 comments

  1. The problem occurs when you have multi author blog, If someone submit article for review this script my delete this post?, Just wondering whats your thought?

    1. Navin – Not necessarily with a multi author blog. Sometimes I line up my posts for publishing with a time gap of 12hrs or a day even. So when they are still in the draft stage, you can still see them up on the Recent Posts widget. 
      Twitter:

  2. Hi Praveen,

    I can see where this would be very irritating for sure.  I don’t believe I’ve run across this before and I’m thankful that I haven’t.  I wouldn’t have known what to do to be honest with you.  Thanks for this tip, I’m sure others will find it very helpful.

    ~Adrienne

Leave a Reply

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

CommentLuv badge

This blog uses premium CommentLuv which allows you to put your keywords with your name if you have had 3 approved comments. Use your real name and then @ your keywords (maximum of 2)