HowTo – Understanding Custom Post Types In WordPress 3.0

Filed under: Industry News,Wordpress Tips

wordpress-logoWith WordPress 3.0 comes the feature of adding custom post types but what are custom post types and what do you need to do to implement them….

First we have to understand what is a Post Type…

Anyone who has used WordPress for a while and looked through not only its dashboard interface but also the database knows that Pages and Posts are almost exactly the same animals. The only real differences between a page and a post would be a few fields in the database that declare an item as a page or post and the options available on the right sidebar in the editor window.

The Post editing page will provide access to the categories menu and while editing a page you will be able to  pick a parent page.

In the database you will see that both pages and posts are stored in the wp_posts table and the post_type field says either page or post.

There are some other differences but that is the basic idea that you will follow when you are creating custom post types.

How can you best use Custom Post Types

This is a really difficult question because there are a lot of different uses for a custom post and there are many options for creating one. The first thing you will have to understand if you are also familiar with Drupal is that laying out a custom post type in your editor … or the interface you will design is defiantly not a simple task at this time.

Lets say you had the idea to make a custom post type for your recipes.

The first thing you would do is define a new post type called recipes and it would show up on the left hand side of your WordPress Dashboard menu right along with your Posts and Pages menus.

Open your functions.php file in your Theme directory or make one there and place the following.

Of course you want to add the <?php …. ?> if you are making one from scratch.

[php]

add_action( ‘init’, ‘create_post_type’ );
function create_post_type() {
register_post_type( ‘recipe’,
array(
‘labels’ => array(
‘name’ => __( ‘Recipes’ ),
‘singular_name’ => __( ‘Recipe’ )
),
‘public’ => true,
)
);
}

[/php]

Ok so that is deceptively easy don’t you think? I do.

Now if you click the menu item to add new Recipe you will be brought to a editor window that has a title bar and the usual editor window but you will see that you are missing a lot of options.

First your custom posts do not have a category menu and this is similar to pages not having a category menu.

The fact is you are going to have to write all of the code or at least reference many options that you are probably not useto dealing with unless you write a lot of plugins or highly specialized Themes for WordPress.

Unlike Widgets that allow you to drag and drop features every option of a custom post type needs to be defined in your functions.php file.

Metaboxes
If you are going so far as to learn how to code a custom post type interface you probably want to take a long look at Metaboxes. This is a feature that has been around since version 2.5 i believe but it is rarely used.

Metaboxes allow you to add the areas below the editor window that you may normally be use to using and it will allow you to define any number of gui type features like checkboxes to assign things like sizes, colors or text boxes to provide urls to videos or google maps…

Conclusion

This can be a very attractive feature if you are willing to devote the time or if you are willing to pay someone to do the work for you. I would strongly suggest that you wait a short period and find some themes that have sample code you can examine.

Remember adding boxes or menu items is one thing but you will have to code everything behind any feature that is normally not available within your standard editor options.

Additionally how you deal with your database including importing of data may change quite a lot.

But in the end you can expect that either a plugin developer or the WordPress core developers will provide a much better way to add custom post types in the future.

Lets face it even people who can code don’t want to spend more time then is reasonable to add lots of new features when it is all spit out at visitors in HTML anyway.

Look for other howtos on this and related subjects in the next few weeks.