Make a New WordPress Loop with query_posts

The “Useful WordPress Function of the Day” award goes to query_posts. This function can be used to:

  • Revise the query that WordPress forms from the URL, so you can change the sorting of posts, exclude certain categories, etc.
    query_posts($query_string . "&order=ASC&category_name=Libraries");

    This takes the current query and sorts it in ascending order, limiting the results to posts in the “Libraries” category.

  • Create custom queries, either for public-facing pages or in administration plugins.
      "category__in" => array(1,3),
      "posts_per_page" => -1,
      "author" => 5

    This query grabs all the posts by author 5 in categories 1 or 3.

After calling query_posts, you can use your standard WordPress loop, along with all the template tags it makes available, in your template or plugin.

jQuery and Ajax in WordPress Plugins – Public Pages

My previous post teaches you how to use jQuery and Ajax for the administration pages in your WordPress plugins. To use them in your user-facing pages requires a few changes.

We’ll use here a simlarly contrived example. Let’s say you use <!--more--> in your longer posts so they don’t fill up too much of your page. Normally, clicking the “Read more…” (or whatever text you use) link takes the user to a separate page with the complete post. In our example, rather than sending the reader to a new page, we’ll make an Ajax request to get the rest of the post and insert it directly into the current page. Continue reading “jQuery and Ajax in WordPress Plugins – Public Pages”

jQuery and Ajax in WordPress Plugins – Administration Pages

This is a quick overview of how to use jQuery and its Ajax functions in WordPress. To get the point across, I’ll use a simple and contrived example. We’ll have an admin screen with a list of categories. Clicking on the name of one of the categories will fetch a list of titles of posts in that category and display them as a sub-list of that category. Continue reading “jQuery and Ajax in WordPress Plugins – Administration Pages”

Command Line PDF Editing

As I’ve mentioned before, Acrobat’s JavaScript API lags far behind other Adobe applications. Its limitations turned a seemingly simple project I was working on into an exercise in futility.


I have a collection of a little over 5,000 PDF files, the output of an OCR job. Each file contains one page of a newspaper. Four pages put together would make one issue. My goal, then, is to take four PDF files (e.g., 1896-09-24_001.pdf, 1896-09-24_002.pdf, 1896-09-24_003.pdf, and 1896-09-24_004.pdf) and merge them together into one file (1896-09-24.pdf). Then repeat 1,300 times or so to get the rest of the issues.

Sounds like an ideal job for a small script. Unfortunately, Acrobat only gives JavaScript access to the file system for opening and saving files. It has no way to read a directory for a list of files, which is rather fundamental to the task at hand.

PyPdf Almost Works

At Jay Luker‘s suggestion, I tried out PyPdf. It seems to do everything I needed. And indeed it would, except it can’t read my PDF files. It does its job just fine with other files, but not these that OmniPage created. It seems the files are missing an attribute that PyPdf looks for, so I end up with a KeyError.

pdftk to the Rescue

So, after much gnashing of teeth, Jason Ronallo suggested I try pdftk, a simple command line tool that can merge and split PDFs, among other capabilities. To merge the issue noted above takes just one line:

pdftk 1896-09-24_001.pdf 1896-09-24_002.pdf 1896-09-24_003.pdf 1896-09-24_004.pdf cat output 1896-09-24.pdf

A simple Python script can just call pdftk repeatedly to take care of the whole collection.

Many thanks, pdftk and #code4lib.

Issue Manager WordPress Plugin

Since we started the Code4Lib Journal, we’ve had a persistent problem of editors accidentally publishing articles early (i.e., before the issue was officially published). Our first workaround for that was to remove the ‘publish_posts’ capability from the editor role. With that capability removed, only the administrator (i.e., me) could publish articles.

That worked, but it was a hassle, as it required me to do what should have been the coordinating editor’s job of publishing each article when it came time to publish a new issue. For that matter, even if permissions weren’t an issue, the publication process would still be cumbersome: open every article in the issue, change its status to “Published”, and save it, doing them all as fast as you can so people didn’t find a half-published issue when they came to the website.

It was to fix these and other problems that I created the Issue Manager WordPress plugin. It connects the publication status of an article to the category it’s in. You can designate a category as “Unpublished”, so that no articles in that category can be published. If you try to publish an article, it’s status changes to “Pending Review”. When it’s time to publish an issue, you change its category’s status to “Published”, which publishes all the “Pending Review” articles in that category. Continue reading “Issue Manager WordPress Plugin”