Retrieving wordpress posts via JSON

Quick piece of code. I ended up using WordPress for my CMS because of my fairly simple needs, the fact that I’ve already played around in the code of some plugins and templates, and the fact that it seems fairly easy to upgrade to something like Drupal if my needs change (that’s the rage now, right?). That said, I had no interest in tying my site too closely to WP or to use a normal template. I had already mostly made the decision to use GWT for my interface, and so really I just needed a way to loosely couple it to content.

The WordPress documentation seems to be getting better constantly, but it still drives me a little crazy that it seems to be mostly examples instead of more succinct and precise definitions. I could be missing something, but it’s not uncommon to find a function’s documentation page made up entirely of something like “here is a list of common usages, but it should not be considered an exhaustive list.” Why not just make an exhaustive list??

Anyway, I wanted a simple php script that could retrieve the latest post and return it, encoded in the JSON format. Google was strangely unhelpful, but I was able to piece together the following and wanted it out there for anyone else looking for similar functionality. It is specifically written to deal with only one post, but it should be trivial to adjust the query_posts parameters (if you can find an exhaustive and well-defined list of them…) and insert the usual while loop, adding each jsonpost to a larger array at the end of the loop. Note that this code will be as slow as any non-cached wordpress page, and your server might not gzip json files by default (mine doesn’t), so think carefully about the latency this will introduce in any AJAX applications.

The code:

// include our wordpress functions
// change relative path to find your WP dir
define('WP_USE_THEMES', false);
// set header for json mime type
header('Content-type: application/json;');
// get latest single post
// exclude a category (#5)
  'posts_per_page' => 1,
  'cat' => -5,
$jsonpost = array();
if (have_posts()) {
  // initialize functions used below
  // construct our array for json
  // apply_filters to content to process shortcodes, etc
  $jsonpost["id"] = get_the_ID();
  $jsonpost["title"] = get_the_title();
  $jsonpost["url"] = apply_filters('the_permalink', get_permalink());
  $jsonpost["content"] = apply_filters('the_content', get_the_content());
  // would rather do iso 8601, but not supported in gwt (yet)
  $jsonpost["date"] = get_the_time('d F Y');
} else {
  // deal with no posts returned
// output json to file
echo json_encode($jsonpost);

I’d like to have this file (and other, larger collections of posts) autogenerated every time I add a new post so that this script is only run once and then the json file can then just be served. I’m going to look into the WordPress cron functions to see how difficult that will be.

(P.S. code formatting above comes via CodeColorer. It does sever-side formatting (via injected tags and classes) which is not always ideal, but gets WordPress to structure the code before returning posts via, say, JSON. The just leaves getting the right CSS definitions included in the host page.)

This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

Leave a Reply

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


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">