Show ONLY WP Recent Published Posts

By Editorial Staff on June 28th, 2010 { 6 Comments }

WP Recent PostsOne of the key features of  WordPress is the ability to do basically anything with it. By just adding a little piece of code to the functions.php, to any of the pages, sidebar or to both many things can be achieved. Lately adding a list of recent posts to a blog has become quite popular and this can be done with just a few pieces of code.

As explained in the Codex, the first thing you need to do is add this piece of code to where you want your recent posts to appear. In this example we’re going to place it in the sidebar.php but you can place it basically anywhere.

<?php wp_get_recent_posts( $num ) ?>

By default $num will pull the 10 most recent posts, but you can add any number you want. Now this piece of code is quite simple and not necessarily too efficient, so you might want to dress it a little bit more like in the example below, so each title becomes a link to the actual post.

<h4>Recent Posts</h4>
<ul>
<?php $recent_posts = wp_get_recent_posts(5); //Shows 5 most recent posts
foreach($recent_posts as $post){
echo ‘<li><a href=”‘ . get_permalink($post["ID"]) . ‘” title=”Look ‘.$post["post_title"].’” >’ .   $post["post_title"].’</a> </li> ‘;
} ?>
</ul>

By running this code you will get the 5 most recent posts but that will include the private ones, drafts and even the published ones, which is not exactly what you want. Last thing anyone wants is to show a lists of drafts and private posts as by default these won’t render, so the solution is very simple.

Locate and open the wp-includes/post.php file. At around line 2039 you will find this piece of code:
/**
* Retrieve number of recent posts.
*
* @since 1.0.0
* @uses $wpdb
*
* @param int $num Optional, default is 10. Number of posts to get.
* @return array List of posts.
*/
function wp_get_recent_posts($num = 10) {
global $wpdb;
// Set the limit clause, if we got a limit
$num = (int) $num;
if ( $num ) {
$limit = “LIMIT $num”;
}
$sql = “SELECT * FROM $wpdb->posts WHERE post_type = ‘post’ AND post_status IN (‘draft‘, ‘publish’, ‘future‘, ‘pending‘, ‘private‘ ) ORDER BY post_date DESC $limit”;
$result = $wpdb->get_results($sql, ARRAY_A);
return $result ? $result : array();
}

From it remove draft, future, pending and private leaving only publish, so the piece of code will look like this:

/**
* Retrieve number of recent posts.
*
* @since 1.0.0
* @uses $wpdb
*
* @param int $num Optional, default is 10. Number of posts to get.
* @return array List of posts.
*/
function wp_get_recent_posts($num = 10) {
global $wpdb;
// Set the limit clause, if we got a limit
$num = (int) $num;
if ( $num ) {
$limit = “LIMIT $num”;
}
$sql = “SELECT * FROM $wpdb->posts WHERE post_type = ‘post’ AND post_status IN (”, ‘publish‘, ”, ”, ” ) ORDER BY post_date DESC $limit”;
$result = $wpdb->get_results($sql, ARRAY_A);
return $result ? $result : array();
}

Save and upload the file again if using FTP or simply save if using the built in WP editor. Now only the published posts will appear when using the wp_get_recent_posts function.

UPDATE 1/11/2012:  As expressed by the many comments below, modifying the core files can be time consuming. Keeping up with the hundreds of updates these files go through every time a new version of WordPress comes out is a task on its own. Instead include the change only on the sidebar in this manner:

/**
* Retrieve number of recent posts.
*/

<h4>Recent Posts</h4>
<ul>
<?php
$args = array( ‘numberposts’ => ’5′, ‘post_status’ => ‘publish’ ); //Shows 5 most published posts only
$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> ‘;
} ?>
</ul>
Monday, June 28th, 2010  |   permalink  |   6 Comments  |  

6 Responses to “Show ONLY WP Recent Published Posts”

  1. newcomer says:

    HI, anyway to order these posts by title or…; I mean how can I use orderby() with wp_get_recent_posts.

    Thanks

  2. You could also add if ($post["post_status"]=="publish") in front of you echo statement in the first example. I find this is simpler than changing the WP core.

  3. Vincenzo says:

    I did this on my blog but the problem still lies in that if you update WordPress and overwrite this file you have to do it again. Kind of annoying to say the least.

    So to avoid this copy this function from posts.php and name it something else in your functions.php file within your theme and use it from there. Then you don’t have to worry about overwriting it or changing it on every update to wordpress.

  4. Editorial Staff says:

    Both very good suggestions! on the first statement don’t forget to add ; at the end
    if ($post["post_status"]==”publish”);

  5. Frank says:

    Great post, this completely fixed my problem.

  6. Hi. Thanks for the info. I found a better solution though. Editing a WP is a bad idea since as posted previous, an update will reset. Try this simple solution using $args & post_status. This will display the last 10 published articles only:

    ’10′, ‘post_status’ => ‘publish’ );
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ){
    echo ‘-’ . $recent["post_title"].’ ‘;
    }
    ?>

Leave your Comment

Powered by eShop v.6