Get it All

It is invariable that, when working with a platform or API or framework written by someone else, you find that things which seem simple enough and obvious enough just don’t actually exist in that chosen system.  Relatively straightforward tasks sometimes slip right through – or else are agreed to be unnecessary – but for your purposes, they are vital. In such a case, generally that is what WordPress allows custom plugins and themes for. Still, as a plugin developer, I’ve found few cases where the underlying data gathering cannot be done through the WordPress API in some way. More complex uses of that data might be in order, hence the plugin, but the data itself is easily obtained through some $wpdb function or another.

But in working on a new fiction-writing website I’m toying with, I discovered one of those sort of oddities about WordPress. Specifically, while there is a built-in function that returns the next post or previous post based on the post you are currently looking at, there is not a specific function which will return a list of X number of posts before or after the current. You can even use a function for pagination – creating a  next_posts_link() and previous_posts_link(), but not the list itself.

And you see, in my case, I would like this contextual list of the next five, current and previous five posts. The story I’m writing is a mystery. The site is, in the best spirit of the William Gibson novels I love and the Nine Inch Nails websites I’ve lurked, a mystery in and of itself. I would prefer not to fall back on the normal modes of blog reading, and instead provide something slightly out-of-phase with what you’d expect. Anyway, it’s all a matter of style and I’ve certainly seen many other site administrators looking for something similar.

Well, I don’t really feel like bundling this up into a plugin because I’ve got what I wanted and frankly feel way too lazy to go any further with this project. But since the gathering of these posts really only requires a single function anyway – how you choose to put them on the page is up to you – I recommend just including it on your theme’s functions.php file anyway. So for those of you looking for something kinda like this solution, bon appetit!

[code language=”php”]
Name: WordPress Contextual Posts
Description: Creates a list of the next and previous five posts from the
currently-viewed post.
Version: 1.0
Author: Thomas J. Belknap
Author URI:

/* Copyright 2006 Thomas J Belknap (email : [email protected])

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
function hn_context_archive($current_post) {
global $wpdb;
if(is_single()) :
// First we select the five posts before
// and after the current post, based on the ID:
$curId = $current_post->ID;
$before = $wpdb->get_results("SELECT ID, post_content FROM $wpdb->posts WHERE ID < $curId AND post_type LIKE ‘post’ AND post_status LIKE ‘publish’ ORDER BY post_date DESC LIMIT 0,5", ARRAY_A);
$after = $wpdb->get_results("SELECT ID, post_content FROM $wpdb->posts WHERE ID > $curId AND post_type LIKE ‘post’ AND post_status LIKE ‘publish’ ORDER BY post_date ASC LIMIT 0,5", ARRAY_A);
$list = array();
// To put the final array in the correct chronological order,
// we need to flip the order of the posts which come after
// our current post:
$after = array_reverse((array)$after);
foreach($after as $key=>$value) :
$list[] = $value;
// thepost variable is set so we can highlight the current
// post in the list. Entirely optional.
$current_post->thepost = true;
// The current post is an object. Convert to array:
$list[] = (array)$current_post;
// As with line 19, we need to be sure that even empty
// result sets are always arrays:
foreach((array)$before as $key=>$value) :
$list[] = $value;
// just get the latest posts
$list = get_posts(‘numberposts=5’);
// Output:
print(‘<ul id="archive">’);
foreach((array)$list as $next) :
$next = (array)$next;
$title = str_split($next[‘post_content’], 40);
$next[‘thepost’] ? $class = ‘ class="current" ‘ : $class = ”;
print(‘<li’.$class.’><a href="’.get_permalink($next[‘ID’]).’" title="’.$title[0].’">’.$title[0].'</a></li>’);
If you appreciate this code, please give it a thought: