If you've got a problem with the world, change yourself. If that's a problem, close your eyes, shut your mouth, and live like a hermit. And if that's a problem...

WordPress Plugin: Recent Posts grouped by year, month, and day

Friday, October 26th, 2007   by Favio

Since a simple list of the recent blog posts looks really boring (ok, maybe the content is the origin of boredom), I wanted to group those posts by year, month and day.

The nearest match to what I wanted was A plugin for adding the post date to wp_get_archives. I based my plugin on Oliver Baty's plugin. Thanks for putting up the code to download, it was really helpful. (^_^)

However, before the coding started, there was a need to conduct a more aggressive search; just in case there was already a Plugin that did the same thing I wished.

The result was negative. It is understandable, since this kind of Plugin affects the presentation layer. Finding a "recent posts" plugin that fits everybody is impossible. Mental note: if I ever get to program a big complex WP Plugin, I'll go with the functional side, instead of the behavioral or presentational one; would be cool to follow this guide too.

Plugin source and documentation

Download the source. It's open. ;)

PHP:
  1. <?php
  2. /*
  3. Plugin Name: favrik Recent Posts
  4. Plugin URI: http://blog.favrik.com/
  5. Description: Display recent posts by date: 1) grouping by year, month,day, and 2) just the date at the left side of the title. Based on the work by <a href="http://www.ardamis.com">Oliver Baty</a>. <a href=" http://www.ardamis.com/2007/06/25/adding-the-post-date-to-wp_get_archives/">Details are here</a>.
  6. Version: 0.1
  7. Author: Favio Manriquez
  8. Author URI: http://blog.favrik.com
  9.     Copyright 2007  Favio Manriquez  (email : favio@favrik.com)
  10.     This program is free software; you can redistribute it and/or modify
  11.     it under the terms of the GNU General Public License as published by
  12.     the Free Software Foundation; either version 2 of the License, or
  13.     (at your option) any later version.
  14.     This program is distributed in the hope that it will be useful,
  15.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.     GNU General Public License for more details.
  18.     You should have received a copy of the GNU General Public License
  19.     along with this program; if not, write to the Free Software
  20.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21. */
  22.  
  23. /**
  24. Most important configuration variable is $group:
  25. 0      -       Just put the date at the left side.
  26. 1      -       Group by year, month, and day.
  27. */
  28. function favrik_recent_posts($args = '') {
  29.     global $wp_locale, $wpdb;
  30.  
  31.     // params fun
  32.     parse_str($args, $r);
  33.     $defaults = array('group' => '1', 'limit' => '10', 'before' => '<li>', 'after' => '</li>', 'show_post_count' => false, 'show_post_date' => true, 'date' => 'F jS, Y', 'order_by' => 'post_date DESC');
  34.     $r = array_merge($defaults, $r);
  35.     extract($r);
  36.    
  37.     // output
  38.     $output    = '';
  39.     $pre       = '';
  40.     $full_date = '';
  41.     $year      = '';
  42.     $month     = '';
  43.     $day       = '';
  44.    
  45.     // the query
  46.     $where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'");
  47.     $join  = apply_filters('getarchives_join', "");
  48.     $qry   = "SELECT ID, post_date, post_title, post_name
  49.               FROM $wpdb->posts $join
  50.               $where ORDER BY $order_by LIMIT $limit";
  51.     $arcresults = $wpdb->get_results($qry);
  52.     if ($arcresults) {
  53.         foreach ($arcresults as $arcresult) {
  54.             if ($arcresult->post_date != '0000-00-00 00:00:00') {
  55.                 $url  = get_permalink($arcresult);
  56.                 if ($group == 0) { // dates at the side of the post link
  57.                     $arc_date = date($date, strtotime($arcresult->post_date));
  58.                     $full_date = '<em class="date">' . $arc_date . '</em> ';
  59.                 }
  60.                 if ($group == 1) { // grouping by year then month-day
  61.                     $y = date('Y', strtotime($arcresult->post_date));
  62.                     if ($year != $y)  {
  63.                         $year = $y;
  64.                         $pre = '<li class="year">' . $year . '</li>';
  65.                     }
  66.                     $m = date('F Y', strtotime($arcresult->post_date));
  67.                     if ($month != $m) {
  68.                         $month = $m;
  69.                         $pre .= '<li class="month">' . substr($month, 0, -4) . '</li>';
  70.                     }
  71.                     $d = date('jS', strtotime($arcresult->post_date));
  72.                     if ($day != $d) {
  73.                         $day = $d;
  74.                         $full_date = '<em>' . $day . '</em>';
  75.                     }
  76.                 }
  77.                 $text = strip_tags(apply_filters('the_title', $arcresult->post_title));
  78.                 $output .= get_archives_link($url, $text, $format,
  79.                                               $pre . $before . $full_date,
  80.                                              $after);
  81.                 $pre = ''; $full_date = '';
  82.             }
  83.         }
  84.     }
  85.     echo $output;
  86. }
  87. ?>

How to use it

Upload to your server and activate the plugin at WordPress admin interface. And try these variations in a template:

PHP:
  1. <ul id="recent-posts">
  2.  <?php favrik_recent_posts('group=1&limit=5'); ?>
  3. </ul>
  4.  
  5. <ul id="recent-posts">
  6.  <?php favrik_recent_posts('group=0&limit=5'); ?>
  7. </ul>

The limit parameter tells how many posts to display at max. The group parameter sets how the post are going to be displayed:
0 - Just put the date at the left side.
1 - Group by year, month, and day.

What now?

I can imagine several ways of customizing a recent posts list. Fortunately, it's an easy task when you have so many code snippets available. Gg.

This entry was posted on Friday, October 26th, 2007 at 3:23 am and is filed under PHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


Leave a Reply





Old and not that old articles

« Boards of Canada - Dayvan Cowboy Music Video

The amazing internet trails »