Quantcast
Channel: ElegantWebDesigns | Photographers & Artists WordPress Themes, Web Design, PSD Album Templates
Viewing all articles
Browse latest Browse all 5

Show ONLY WP Recent Published Posts

$
0
0

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>

Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images