Archive for May 4th, 2005

Bugs and Free Software

Wednesday, May 4th, 2005

Here’s a bug I noticed with wordpress. If you write a post in wordpress, but save it as a draft without posting it, these articles will still show up in your “Recent Posts” item in the sidebar (using the Kubrick Theme). I’m not sure if this is a bug in the Theme or in Wordpress itself. The Kubrick sidebar calls wp_get_recent_posts() to get the list of recent posts. However wp_get_recent_posts() queries the database for all posts including published, drafts, and private posts. When you then click on the link to a draft article, you get a 404 not found error message. Quite annoying, and definitely a bug.

The solution is to create a new function in functions-post.php called wp_get_recent_published_posts(). This function is a copy of wp_get_recent_posts() with one thing changed: the MySQL query for the post should not include posts whose status is draft. This is accomplished by changing the following line:

        $sql = \"SELECT * FROM $wpdb->posts \
        WHERE post_status \
        IN ('publish', 'draft', 'private') \
        ORDER BY post_date DESC $limit\";

to

        $sql = \"SELECT * FROM $wpdb->posts \
        WHERE post_status \
        IN ('publish', 'private') \
        ORDER BY post_date DESC $limit\";

Then we can change the sidebar.php from the Kubrick theme to call wp_get_recent_published_posts() instead of the buggy wp_get_recent_posts().

Why create a new function instead of fixing the buggy one? There may be other functions that call wp_get_recent_posts() which depend on that buggy functionality. By creating my own function, and calling that, I can localize the fix to the place where it is manifested. I am able to fix the bug without introducing new ones.

Bugs are inevitable in software. There is an axiom that states, “all non-trivial piece of software have bugs.” And it’s corrollary “if a piece of software is bug free, it must be trivial.” This applies to both free (as in liberty) and proprietary software. But the beauty of free software, is that the source code is there for you to modify. You are free to use and modify it to your heart’s content. If wordpress was a piece of proprietary software, source code would not be available, and I would be left at the mercy of the software publisher. They can decide to fix my bug if I was a big enough customer with many lawyers, or if I was a simple individual they could simply ignore the bug and let me suffer since there is nothing I can do about it. There is such a thing as a free lunch after all.

-- Posted in Geeks Paradise