Show posts one day at a time

The code below retrieves an array of dates which have posts and shows the posts for one day at a time.

// This code is designed to show WordPress posts by date.
$pageurl = get_permalink();  // Save this for pagination
$sql = "SELECT distinct DATE_FORMAT(post_date,'%Y%m%d') as pdate FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' AND post_date <= NOW() ORDER BY pdate DESC";
$dates = $wpdb->get_col($sql);
if ($dates) {
   $showdate = ($_GET['showdate']) ? $_GET['showdate'] : $dates[0];
   query_posts('year=' . substr($showdate,0,4) . '&monthnum=' . substr($showdate,4,2) . '&day=' . substr($showdate,6,2));
   if (have_posts()) {
     echo '
<div class="posts">';
     echo "
<h2>Posts for $showdate</h2>
";
     while (have_posts()) {
       the_post();
       echo '
<div class="post">';
       the_title();
       echo ' - ';
       the_time('F j, Y');
       echo '</div>
';
     }
     echo '</div>
';
     echo '
<div class="nav">';
     $i = array_search($showdate,$dates);
     $ndx = $i + 1;
     if ($ndx < sizeof($dates)) {
       $href = add_query_arg('showdate',$dates[$ndx],$pageurl);
       echo "<a href="$href">&laquo Previous Date</a>";
     }
     $ndx = $i - 1;
     if ($ndx >= 0) {
       echo '   ';
       $href = add_query_arg('showdate',$dates[$ndx],$pageurl);
       echo "<a href="$href">Next Date »</a>";
     }
     echo '</div>
';

   }
}
?>

Leave a Reply

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

*