A Painful Trip Through Wordpress
You may have noticed a bit of mess in the posts to my blog today. I've just started a process where I'm going to either publish old draft posts, or delete them. My first post was a post from a conversation I had with my wife, Memory Constipation (not memory leaks). It was actually a bit exciting having this happen.
- I edited the article, and clicked publish, went to my blogs front page, and noticed no listing
- I looked at the posting, and noticed the date was "June 2006"… hmmm I guess I started writing the article then…
- I changed the date to now, went to the front page, and still no article…
- remembering an AJ rant at work about future posts, I thought that perhaps I was future posting. I set the date to yesterday, and noticed the post. YAY!! I then did my refresh trick to try and see the post on JavaBlogs, but no joy….
- So I played with the date, and finally got it to about now on the post.
- I went back to JavaBlogs, and noticed that the post now had appeared twice d'oh… what was worse is that the first permalink (with yesterday's date) was broken NASTY.
-
so I went and created a new post with the same title and yesterdays date. Unfortunately WordPress forces the article names to be unique, so this didn't work. People trying with the original posting to JavaBlogs would still get a 404
- By now I was getting a bit annoyed with WordPress, but wasn't about to give up. I tried forcing the name of the post, but that didn't work, so had to head into hacking the permalink approach. The rest of this post shows what I did (warning this was a hacky approach to PHP and updating wordpress).
So the problem was that when wordpress was looking for the post it was using the date of the post, and the unique name to look for the post. Given that there was no post with the right name on the date, no post was found, so wordpress returned a 404. My work around is to leave the query code as close to the original as possible, only adding a check at the end, that will remove date the date parameters if no posts were found. The following code goes at the end of the get_posts function in the WP_Query class.
// if there aren't any posts returned by the query, name and year parameters were specified, then
// we are probably dealing with a post that has moved.
// so lets go ahead and try a query without any date fields.
if (sizeof($this->posts)==0 && isset($q["name"]) && isset($q["year"])) {
unset($this->query_vars['year']);
unset($this->query_vars['monthnum']);
unset($this->query_vars['day']);
return $this->get_posts();
}
It's a nasty hack, but it does the job, now the following urls, all return my page.