Working With WordPress 3.0's Custom Post Types

Just last week I finally got the chance to try the new custom post types that came with WordPress 3. For some reason, I never thought about using them because I was so used to using custom fields for everything. After setting up some pre-made templates for a few clients that used custom post types, I decided to check it out and try them for myself.

It turns out custom post types are easy to setup, understand and have made my code MUCH cleaner! I learned a lot about this new function in just a short time, so I thought I’d share it with everyone.

Setting Up Custom Post Types

Before you can do anything, you have to first set up your custom post type. In your functions.php file you need to add:

register_post_type('praise', array(
	'label' => __('Praise'),
	'singular_label' => __('Praise'),
	'public' => true,
	'show_ui' => true,
	'capability_type' => 'post',
	'hierarchical' => false,
	'rewrite' => false,
	'query_var' => false,
	'supports' => array('title', 'editor', 'author'),
));

This sets up a custom post type called “praise” and will add a new section to the top of the admin navigation area called “Praise”. From there, you can begin to add new posts.

Adding Custom Fields

The current setup above allows you to add new content, much like the regular “Posts” section does. However, right now it doesn’t allow you to input custom fields, which hinders our ability to truly customize our design.

Luckily, WordPress allows us to add more sections to our custom post type, placed in the “supports” array. You can add your taxonomies, versions and more. To add custom fields, simply add this to the supports array:

register_post_type('praise', array(
	'supports' => array('title', 'editor', 'author','custom-fields'),
));

Using Custom Post Types In A Query

The next thing I needed to do was to actually display the posts in a query loop so I could list them on a page, instead of an archive page. This was actually really easy to go with the standard ‘query_posts’ function:

<?php query_posts('post_type=praise'); if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

Simply change “praise” to whatever you named your custom post type in function.php file.

Giving Custom Post Types Pretty Permalinks

The next problem I encountered were the links that were created with custom post types. Unfortunately, it doesn’t follow WordPress’s permalink structure, and comes up with an ugly and HTML5/XHTML1.0 invalid use of ampersands in the URL.

With the addition of a few more lines to our register_post_type function though, we can add pretty permalinks:

register_post_type('praise', array(
	'rewrite' => array(
    	'slug' => 'praise',
    	'with_front' => FALSE,
  ),
));

You can change the slug name to be whatever you want the proceeding folder to be in the URL.

Now, this changes all the actually URLs on the site, but if you try to click on them, it will actually take you to the 404 error page. So after our register_post_type function, we need to add:

global $wp_rewrite;
$wp_rewrite->flush_rules();

Which will flush our rewrite rules and will allow the new permalinks to work.

Custom Templates

What’s cool about custom post types, is that you’re not limited to the normal single.php file for templating. If you want to use a custom template, simply name it single-praise.php (or whatever name you chose for yours) and WordPress will automatically pick it up and use that file instead.

Want To See An Example?

Check out one of my client’s current site in progress to see the use of custom post types (in praise and pictures sections).

If only I had time to recode my own site now :(.

The Power

What makes custom post types REALLY powerful is when you combine it with custom taxonomies, custom fields and the rest of WordPress’s CMS functions. Every site I code works completely different from the previous one, and I’m hoping with these new features my sites can finally be somewhat similar in the bones. 🙂