One of the most confusing features of WordPress is custom post types. It is also a useful, powerful, and easy feature to implement and use after you understand how it works. WordPress has five default post types:
A post type is really a type of content stored in the wp_posts table in the WordPress database. The post type is stored in the wp_posts table in the post_type column. The information in the post_type column differentiates each type of content so that WordPress, a theme, or a plugin can treat the specific content types differently.
When you understand that a post type is really just a method to distinguish how different content types are used, you can investigate custom post types.
Say you have a Web site about movies. Movies have common attributes — actors, directors, writers, and producers. But say you don't want to store your movie information in a post or a page because it doesn't fit either content type. This is where custom post types become useful. You can create a custom post type for movies and apply the common attributes of actors, directors, and so on. A theme can handle movies differently than a post or a page by having a custom template for the movies post type and create different styling attributes and templates for the movies post type. You can search and archive movies differently than you can with posts and pages.
Here's how to create a simple custom post type in WordPress by adding these lines of code into the Theme Functions template file (located in the theme file and called functions.php):
add_action('init','create_post_type'), function create_post_type() { register_post_type( 'movies', array( 'labels' => array( 'name' => ('Movies'), 'singular_name' => ('Movie'), 'rewrite' => array('slug' => 'movies'), ), 'public' => true, ) ); }
Let's see what's going on in the code:
Figure 7-5 shows how the Custom Post Type page and menu item look in the Dashboard.
Figure 7-6 shows a custom post type on a Web site.
Many other arguments associated with register_post_type give this function its real power. For full documentation on all the arguments and the use of this function, check out http://codex.wordpress.org/Function_Reference/register_post_type. (Custom post types are also discussed in detail in Book VI, Chapter 6.)