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!
/*
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: http://holisticnetworking.net
*/
/* Copyright 2006 Thomas J Belknap (email : tbelknap@holisticnetworking.net)
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
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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;
endforeach;
// 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;
endforeach;
else:
// just get the latest posts
$list = get_posts('numberposts=5');
endif;
// 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>');
endforeach;
print('</ul>');
}
If you appreciate this code, please give it a thought:
I’ve modified a plugin that sends messages via Ping.FM whenever you post an article on a WordPress blog. The aim is to have it post a message whenever I post to any one of my blogs on this network, without me needing to set up each blog individually.
No tag for this post.
It recently became necessary for me to work with the WP-Cron API for a local band website. Those of you who’ve done any work with WP-Cron have no doubt noticed that the way cron jobs are held in the database can be a bit cumbersome to work with.
By this I mean: since the key under which all the arguments is held is a hash of all the arguments, they can be a bit difficult to get at when trying to reveal them or work with them in any way other than the direct API. I’m sure someone more familiar with the inner workings of the API could provide a decent explanation of why this is so, but I certainly cannot.
So in working with crons – I needed a way to be able to quickly visualize the crons setup by my plugin, which emailed a post like a mailing list, to delete any jobs sent in error – I had to come up with some functions that properly dealt with the WP-Cron array. I submit these functions to you now in hopes that this example might help someone else down the line:
function bhd_get_schedule() {
$crons = _get_cron_array();
if ( empty($crons) )
return false;
foreach ( $crons as $timestamp => $cron ) {
if ( isset( $cron['bhd_cron_send_hook'] ) ) {
foreach($cron['bhd_cron_send_hook'] as $md => $stuff) {
$result[$timestamp] = $stuff['args'];
}
}
}
return $result;
}
Notice that this is really just a modification of the get_schedule() function already a part of the wp-cron API. However, what I’ve done in this case is simply provide a convenient way to bypass the hash and return a list of crons and their arguments, so they can be worked with conveniently. In my case, I only wanted one particular cron hook. If you have more than one, you might make the function take an argument that it then supplies to the isset line.
function bhd_delete_jobs($form) {
$jobs = bhd_get_schedule();
$deleted = 0;
foreach($form['cron_job'] as $input=>$timestamp) {
if ( wp_unschedule_event($timestamp, 'bhd_cron_send_hook', $jobs[$timestamp]) ) {
$deleted++;
}
}
return($deleted);
}
<?php if($jobs = bhd_get_schedule()) { ?>
Below is a list of the currently scheduled mailing list deliveries, meaning each represents an email with a batch of fifty mailing list members as recipients.
<?php } ?>
I’ve downloaded and installed a plugin that is designed to display code in a more helpful way, not ironically called “Code Markup,” and I’m playing around to see how things work. It’s supposed to be able to differentiate between different types of code and color-code the markup accordingly. Let’s find out.
Tags: Code, Plugin Concepts, Testing, WordPressThis is a paragraph inside a basic HTML wrapper.This is a paragraph inside a basic HTML wrapper.foreach($boogers as $booger) { flick($booger); }
There seems to have been a small problem with the Title to Tags plugin, as I inadvertently created a function without an “hn_” prefix. So, there was a conflict with another plugin. So, I’m recommitting a new version of the Title to Tags plugin, version 1.2.
Tags: Plugin, title_to_tags, WordPress
I’m playing around with WordPress MU, and will need to be doing a lot of hacking the MU code in order to get what I want out of MU. So, as long as I’m working on projects, I plan to design around general concepts instead of site-specific requirements and post my projects and work here.
My first order of business is to determine how, exactly, I’m going to create the “classes” concept I’ve been toying with. In terms of the dB, this shouldn’t be too taxing an undertaking. But I’ve never bothered to create any Administrative plugins for WordPress before (I’ve written a few popular display plugins in the past). So finding the hooks I need to get this done will be my first challenge.
Oh, yeah. I think perhaps I’ll start a category exclusively for revelations concerning hooks, generally. I find that other sources tend to be incomplete, so I hope I can at least add something to the conversation and help the next guy figure out his project.
No tag for this post.