Separating Content Types

One of the major goals for the 2007 redesign was separating the different types of content posted on the site.

Over the years, post sizes have decreased. I’m not sure when, but once upon a time I implemented what are commonly called “Asides” as a type of short quick post. Instead of calling them “Asides” though, I’ve always called them “Quickie” posts here. These Quickies were styled in a different way on the home page and through the archives so they would stand out.

I had been using a hacked up plugin to use WordPress category functionality as tags. Whenever I “tagged” a post as a Quickie, it received a different style and everything else was styled normally. I never really implemented a way to view all of the “normal” longer length posts.

I’ve been using Twitter more and more since November of 2006, so also wanted a way to pull in my twitter updates. For awhile, I was using Twitter Tools to create a daily digest post of the Twitter updates for that day. It was kind of ugly.

So I have 3 different types of content getting posted to the site. Full posts, quickie posts, and twitter updates. WordPress 2.3 added tagging support which turned out to be a lifesaver. I no longer had to use categories as tags. I could simply live with two categories on the blog; Entry, for full length posts and Quickie. Then I’d simply use WordPress’ built in tagging support for all of my tags.

The first step was to convert all of the categories I was using as tags into the real deal. WordPress has an interface that does this automatically for you. You can access it by visiting wp-admin/admin.php?import=wp-cat2tag in your WordPress Admin area. WordPress will not allow you to convert all of your categories to tags, so if you check them all, your default category will be both a tag and a category.

The WordPress Category to Tag Converter does not provide a check all option and I wasn’t exactly going to go through and check over 700 boxes. I found out the Firefox Web Developer Add-on could do this. If you have it installed…

  1. Right click in an area around one of the check boxes.
  2. Select Web Developer.
  3. Select Forms.
  4. Select Populate Form Fields.

All of the check boxes will be selected. Then click the Convert button and WordPress will magically convert all of the selected categories into tags.

I forgot to deselect quickie from being converted, so now I had it as both a category and a tag. Using the Simple Tags plugin I was able to easily delete the tag quickie and was still left with the category quickie.

The last stage was getting the Entry category. It was easy enough to create the category in WordPress, but I needed a way to have the Entry category applied to all posts not already categorized with Quickie. After digging around the WordPress database, I figured out how categories were being stored and came up with some code to do the trick.


<?php

//Any uncategorized posts get placed in the category set with $my_cat)

//Category counter gets updated as well



	$my_cat = 693;

	$my_post_ids = $wpdb->get_results("SELECT DISTINCT `id` FROM $wpdb->posts");

	foreach ($my_post_ids as $my_post_id) {

		$my_categories = get_the_category($my_post_id->id);

		if (empty($my_categories)) {

			$wpdb->query("INSERT INTO wp_term_relationships (`object_id`, `term_taxonomy_id`) VALUES (" . $my_post_id->id . ", ' . $my_cat . ')");

			$wpdb->query("UPDATE wp_term_taxonomy SET `count`=`count` + 1 WHERE `term_taxonomy_id` = ' . $my_cat . '");

		}

	}

?>

The code loops through all of the posts and if the post is not categorized, it adds a category to the post and increments the counter for the category. If you would like to use this code…

  1. Make sure you are using WordPress 2.3.1. I have no idea if this will work for 2.3, so use at your own risk.
  2. BACKUP your WordPress database! This code will make changes to the database that cannot be reversed.
  3. Copy the above code into your theme’s header.php right after .
  4. Go to Manage->Categories in your WordPress Admin.
  5. Write down the ID corresponding to the category you want applied to posts that are not categorized. The ID for my Entry category was 720.
  6. Access your database with phpMyAdmin or some other tool.
  7. Browse the wp_term_taxonomy table.
  8. Find the record where the term_id field matches the ID from step 5. Write down the corresponding term_taxonomy_id. This was 693 for me.
  9. Edit the code you copied to header.php. Replace 693 in this line $my_cat = 693; with the number you wrote down in step 8.
  10. Upload header.php to your site and load your web site. This will force the code block to run.
  11. Once the page has fully loaded, edit header.php again, remove the code, and upload again. This should be your original header.php.

This isn’t the most elegant way of running a block of PHP code, but it was a quick and dirty solution. Use it if you want.

So now I finally had the structure working. Full posts have their own category of Entry, short posts have a category of Quickie, and Twitter updates are pulled directly from twitter.com. On the home page of the blog, the main content area lists the entire contents of the latest Entry post, excerpts of the previous 4 Entry posts, and a link to the Entry Archives. The home page’s left sidebar displays the last 5 Twitter updates, the last 5 Quickie posts, and links to view more of each.

When I’m not writing full posts, the home page is constantly being updated with new Twitters and Quickies. In the past, this would have pushed the full posts off the list.

In order to pull out two sets of posts from the database, I use two custom instances of “The Loop” as we call it in WordPress. This is actually quite simple. Before , simply add where 16 is the category ID of the category to display, and 5 is the number of posts you want displayed. Run through your WordPress loop and then you can add another loop somewhere else. Just modify the category and number of posts to display.

Update: A day after posting this, I decided to stop using Twitter.