I haven’t upgraded any of my blogs to WordPress 2.1 yet as I don’t feel like fixing anything that might break when I do. Pure laziness I know. Last week though, a friend asked me for some help on a site he was developing for a client. He was using WordPress as a CMS for the entire site rather than just the blog. He created a new template for the home page and in the sidebar he wanted to show the snippets of the most recent posts. There are plugins for this but he wanted to avoid using them. I knew there was a function that would show a snippet of the post with a “Read More” link called the_excerpt. This function only works within the_loop which wouldn’t work in this case. So, I needed to actually write some code *gasp* and the query I used was:
$news=$wpdb->get_results("SELECT ID,post_title, SUBSTR(post_content, 1, 30) as post_content FROM wp_posts
WHERE post_status= 'publish' ORDER BY ID DESC LIMIT ".$how_many);
This didn’t work though. It got not only the posts but also the pages. I didn’t have access to his site directly so I tested this on my version of WordPress 2.0x. It worked fine. This is when I asked him what version he was using and he said 2.1. Which of course made sense because it was a new site, might as well use the latest and greatest, right?
So I tried to find out why it wasn’t working and what the changes were in 2.1 that affected pages but apparently my google search terms sucked and neither of us could find it. So I told him to open up his db and tell me what it said in the post_status column in the wp_posts table. See, in previous versions of WordPress, in order to differentiate between a post and a page, it was dependent on the post_status column. For a published post, the column had a value of “publish” and for a page it had the value “static” in the column. All of his posts and pages both had values of “publish”. So this was obviously where the problem was. But now we needed to find out where exactly an entry was defined as a post or a page. Ultimately, we got it narrowed down to the post_type column. Previously, this column wasn’t used at all, at least from what I could see. So now, in order to differentiate between a post and a page, a post gets the value post_type=”post” and a page gets post_type=”page”. Makes you wonder why it didn’t do this in the first place!
I’m sure this affects a lot of plugins; I know it affects my Ask Me plugin!