Adding custom post types to WordPress 3

In the latest version of the WordPress blogging platform, the developers have (finally) introduced Custom Post Types, allowing it to become more of a CMS without extensive use of plugins. Unfortunately, after installing the latest version, you’ll probably have noticed that there doesn’t immediatly appear to be any way to create your own. That’s because there is no UI to do this with – doh. The way to do this is to use the recently introducded method register_post_type().

The Example

Lets say you are running a blog that will have podcasts as a content type every so often, but you don’t require a heavyweight plugin like PodPressCustom Post Types are the perfect solution.

The Code

Take a look at the following code:
[box icon="code"]register_post_type(‘podcast’, array(
‘label’ => __(‘Podcasts’),
‘singular_label’ => __(‘Podcast’),
‘public’ => true,
‘show_ui’ => true,
‘hierarchical’ => false,
‘query_var’ => false,
‘supports’ => array(‘title’, ‘editor’, ‘author’)
));[/box]
Whack that straight in your theme’s functions.php file and voila – you’ll see the new Podcast custom post type right there listed underneath the Comments button. So what did this code do exactly?

The Parameters

As you can see, all we’re really doing is calling the new register_post_type() method and sending through some parameters:

  • Label is pretty straight forward, what do you want the custom post type to be displayed.
  • Singular label is also intuitive, what do you call ONE of your post types.
  • Public is a meta argument to determine whether your post type will be accessible via search, menu’s etc… (more about this on wordpress docs)
  • Show UI - set to true to show the custom post type on the back end. False can be set to allow extra content to determine how the custom post type is displayed in the back end.
  • Hierarchical is a parameter to determine whether each custom post can have or be a parent of another.
  • Supports allows you to determine what content creation fields are used when creating or editing a custom post:
    • ‘title’
    • ‘editor’ (content)
    • ‘author’
    • ‘thumbnail’ (featured image) (current theme must also support post-thumbnails)
    • ‘excerpt’
    • ‘trackbacks’
    • ‘custom-fields’
    • ‘comments’ (also will see comment count balloon on edit screen)
    • ‘revisions’ (will store revisions)
    • ‘page-attributes’ (template and menu order) (hierarchical must be true)

 

The Results

Shown below: the new “Podcasts” custom post type. Now start filling that bad boy with some content!

4 Responses to Adding custom post types to WordPress 3
  1. PKGMULLET Reply

    Do you know how to include the podpress meta box in a custom post type?

  2. [...] @andygirvan Social MediaRelated Posts Adding custom post types to WordPress 3WordPress 3.0 – What... andygirvan.com/2011/06/is-wordpress-the-right-tool-for-the-job

Leave a Reply

Your email address will not be published. Please enter your name, email and a comment.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>