Ian Purton

Ian Purton’s Notes

WordPress Recent Posts Plugin

  • PublishedAugust 4th, 2006
  • Authorian.purton
  • CategoriesWeb Marketing

This PHP code will give your WordPress blog a recent posts list as I have on my sidebar to the right.

It’s not actually a plugin, you need to cut and paste this code into the sidebar.php of your theme.

Recent Posts

<?php
function get_recent_posts ($num_posts = 10)
{
  global $wpdb, $tableposts, $post, $tablepost2cat;

  if (!isset($tablepost2cat)) $tablepost2cat = $wpdb->post2cat;
  if (!isset($tableposts)) $tableposts = $wpdb->posts;

  $orderby = "$tableposts.post_$orderby";

  $now = current_time('mysql');
  $sql = "SELECT DISTINCT * FROM $tableposts ";
  $sql .= "WHERE $tableposts.post_date <= '$now' AND ( $tableposts.post_status = 'publish' ";
  $sql .= "OR $tableposts.post_status = 'sticky' ";
  $sql .= ") ";
  $sql .= "GROUP BY $tableposts.ID ORDER BY $tableposts.post_date DESC";
  $sql .= " LIMIT 0, $num_posts";

  $posts = array();
  $posts = $wpdb->get_results($sql);
  if (empty($posts)) return;

  echo '<ul>';

  foreach ($posts as $post) {

   $title = the_title('','', false);
   echo '<li>';
   echo '<a href="'.get_permalink().'" title="View'.htmlspecialchars(strip_tags($title)).'">'.$title.'</a>';
   echo '</li>';
  }

  echo '</ul>';

 } //end function get_recent_posts()

 get_recent_posts();
?>

"WordPress Recent Posts Plugin" was published on August 4th, 2006 and is listed in Web Marketing.

Follow comments via the RSS Feed | Leave a comment | Trackback URL

WordPress Recent Posts Plugin: 9 Comments

  1. This is a great help! Thanks for this mate

  2. Hi. This is a nice little feature. However, I notice it picks up pages as well as posts. Do you think it could be refined to show posts only?

  3. Thanks,

    It works great on my blog. How did you get it to work with comments as well?

  4. Thanks. This worked perfect and was exactly what I was looking for.

  5. Chris

    Nice, although I have the same problem as Bock the Robber– how can I exclude pages and only include posts??

  6. [...] involved. No uploaded files or manipulating funny codes. Just copy and paste the code from this wordpress recent plugin page into your sidebar.php, and that’s it. All done. It was the simplest add-on I’ve ever [...]

  7. If you want to only include posts, add the following code to the script on the line BEFORE “$sql .= “GROUP BY $tableposts.ID ORDER BY.”

    Code to add: $sql .= ” AND $tableposts.post_type = ‘post’ “;

  8. Just a note … I added the following line to your code in order to elimate “pages” from being displayed. On the site that I was working on, I only wanted “posts” to display …

    After the line that contains the “WHERE” clause, I added:

    $sql .= “AND $tableposts.post_type = ‘post’ “;

    Great function! Thanks!

  9. Matt Blank

    Great script! But can it be altered so that it will not list the post your are currently on?

    i.e I’m viewing my third latest post, so the list on the left will show posts 1 and 2, then 4 - 11??

    Thanks!
    Matt

Leave Your Comment

(required)
(required)
 

Subtraction Wordpress Theme by Ian Purton