First Impressions of Custom Post Type

One of the new very interesting things in WordPress 3.0 are individual post-types you can implement with little effort. Back then, you had to expand the database and write your own interface for it, now you just have to add a few lines of code - of course this is just the current state, which can be change until the final release.

After Justin had been playing with these types, we check out the possibilities of types for "Movies".

Simple Solution

function post_type_movies() {
	register_post_type( 'movies',
                array( 'label' => __('Movies'), 'public' => true, 'show_ui' => true ) );
	register_taxonomy_for_object_type('post_tag', 'movies');
}
add_action('init', 'post_type_movies');

default custom post type

More parameters for meta-boxes

Of course there are a number of parameters for this function and so the behavior and appearance of the corresponding edit page can be controlled quite easily, a small sample with additional meta boxes:

function post_type_movies() {
	register_post_type(
                     'movies', 
                     array('label' => __('Movies'), 
                             'public' => true, 
                             'show_ui' => true,
                             'supports' => array(
                                        'post-thumbnails',
                                        'excerpts',
                                        'trackbacks',
                                        'custom-fields',
                                        'comments',
                                        'revisions')
                                ) 
                      );
	register_taxonomy_for_object_type('post_tag', 'movies');
}
add_action('init', 'post_type_movies');

The default arguments

// Args prefixed with an underscore are reserved for internal use.
$defaults = array(
    'label' => false,
    'publicly_queryable' => null,
    'exclude_from_search' => null,
    '_builtin' => false,
    '_edit_link' => 'post.php?post=%d',
    'capability_type' => 'post',
    'hierarchical' => false,
    'public' => false,
    'rewrite' => true,
    'query_var' => true,
    'supports' => array(),
    'register_meta_box_cb' => null,
    'taxonomies' => array(),
    'show_ui' => null
);
  • label - A descriptive name for the post type marked for translation. Defaults to $post_type
  • public - Whether posts of this type should be shown in the admin UI. Defaults to false
  • exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true if the type is not public, false if the type is public
  • publicly_queryable - Whether post_type queries can be performed from the front page. Defaults to whatever public is set as
  • show_ui - Whether to generate a default UI for managing this post type. Defaults to true if the type is public, false if the type is not public
  • inherit_type - The post type from which to inherit the edit link and capability type. Defaults to none
  • capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post"
  • edit_cap - The capability that controls editing a particular object of this post type. Defaults to "edit_$capability_type" (edit_post)
  • edit_type_cap - The capability that controls editing objects of this post type as a class. Defaults to "edit_ . $capability_type . s" (edit_posts)
  • edit_others_cap - The capability that controls editing objects of this post type that are owned by other users. Defaults to "edit_others_ . $capability_type . s" (edit_others_posts)
  • edit_others_cap - The capability that controls publishing objects of this post type. Defaults to "publish_ . $capability_type . s" (publish_posts)
  • read_cap - The capability that controls reading a particular object of this post type. Defaults to "read_$capability_type" (read_post)
  • delete_cap - The capability that controls deleting a particular object of this post type. Defaults to "delete_$capability_type" (delete_post)
  • hierarchical - Whether the post type is hierarchical. Defaults to false
  • supports - An alias for calling add_post_type_support() directly. See add_post_type_support() for Documentation. Defaults to none
  • register_meta_box_cb - Provide a callback function that will be called when setting up the meta boxes for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback
  • taxonomies - An array of taxonomy identifiers that will be registered for the post type. Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type()

Including Custom Taxonomies

In the following example we include in our Post-Type a Taxonomy with two possibilities; own Tags and categories for Post-Type Movies, the classical tag, without hierarchy and one as category, tag with hierarchies.

function post_type_movies() {
	register_post_type(
                'movies', 
                array(
                        'label' => __('Movies'),
                        'public' => true,
                        'show_ui' => true,
                        'supports' => array(
                                     'post-thumbnails',
                                     'excerpts',
                                     'trackbacks',
                                     'custom-fields',
                                     'comments',
                                     'revisions')
                )
        );
	
	register_taxonomy( 'actor', 'movies', array( 'hierarchical' => true, 'label' => __('Actor') ) ); 
	
        register_taxonomy( 'production', 'movies',
		array(
                         'hierarchical' => false,
			 'label' => __('Production'),
			 'query_var' => 'production',
			 'rewrite' => array('slug' => 'production' )
		)
	);
}
add_action('init', 'post_type_movies');

Definitely a very interesting and useful feature which provides many possibilities to play around with.