WordPress

Adding Your Own Custom Post Type

From WordPress 3.0 and on, you can now create your own custom post type. Doing so makes WordPress much more powerful as a content management system as you now are not just limited to posts and pages, but can add any type you want.

What can you do with Post Types?

As I mentioned in the intro, post types can be used to extend WordPress to be more powerful. Instead of having to create all content as posts and pages you can create your own set of posts-like types that you can call whatever you want.

One example would be this site. Since the site was setup prior to WordPress 3.0, I use a complex system of categories and sub-categories in order to display tutorials, reviews and quick-tips in their right places, including other less complex page templates to display the content I want. This is all because both tutorials, reviews and quick-tips need to be posts.

With WordPress 3.0 and the Custom Post Types, you can have different sections, with different fields and options, available in the admin panel to post from.

Another reason why you might want to implement this is the options, being able to remove unnecessary fields from the post edit screen can do wonders to help clients manage websites. If you never need to use the excerpt box, hide it from the screen. Maybe custom fields just confuse, hide them if not necessary. Options here are endless.

Adding Your Own Custom Post Type

To add your own custom post type in WordPress 3.0, all you need to do is add some code to your functions.php file.

The basic function is called register_post_type() and has a bunch of different arguments, all of which you can look up in the WordPress codex.

Registering a basic post type
By adding the following code to your functions.php file (within the tags) you will have a basic new post type:

1
2
3
4
5
6
7
8
9
10
11
12
add_action( 'init', 'new_post_type' );
function new_post_type() {
  register_post_type( 'site_product',
    array(
      'labels' => array(
        'name' => __( 'Products' ),
        'singular_name' => __( 'Product' )
      ),
      'public' => true
    )
  );
}

A couple of things about the code. Firstly we run the action that will add the whole function to WordPress. Secondly, when starting out the register_post_type function, we need to give the new post type a name. Even though naming your post type something simple, try to make it more unique (so that it never conflicts with a plugin or other theme) by prefixing your company initials or similar. Remember not to have this exceed 22 characters!

Next up we call one of the parameters of the function, the labels array which will name all the terms and phrases used in the admin interface. This is just a couple of the terms and if you are adding this, I would advise you to go ahead and insert more the labels as you can find in the full list.

Finally we define that this is indeed a public post type that will show up on the site when called using query posts as well as in the admin panel.

Registering a more complete custom post type
If you are going to be using this on a live website or especially on a client website, you will need to do a little more work to ensure the custom post type works more or less as expected. Here is a slightly longer snippet of code which will give us more options (I have left out more labels in this example):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
add_action( 'init', 'new_post_type' );
function new_post_type() {
  register_post_type( 'site_product',
    array(
      'labels' => array(
        'name' => __( 'Products' ),
        'add_new' => __( 'Product' )
      ),
      'public' => true,
      'rewrite' => array('slug' => 'products'),
      'supports' => array('title','editor','thumbnail','custom-fields'')
    )
  );
}

What differs here are two additional parameters, the rewrite and supports arrays.

What the rewrite will do is enable you to have a custom post slug for the post type. If you normally have a permalink base of /blog, then this would normally default to /blog/post_type_name (in this case /blog/product). By inserting your own slug here, you can have it be /products instead.

The second parameter is supports, which adds information about what type of windows should show in the new and edit post screens. Available ones are: ‘title’, ‘editor’, ‘author’, ‘thumbnail’, ‘excerpt’, ‘sticky’, ‘trackbacks’, ‘custom-fields’, ‘comments’, ‘revisions’, ‘page-attributes’.

The thumbnail value will require the theme to also have support for post thumbnails. If you add the ‘page-attributes’, you will also need to add support for hierarchical structure in the post type.

Just add the code snippet and change out the values to suit your needs. You can add as many new custom post types as you wish, making your life a little bit easier in managing a more complex site with WordPress.

Read more…

This is an introduction to help you add your own custom post types in WordPress 3.0 or later. For more information on the functions used and all the parameters available to you, you should refer to the WordPress Codex which is very detailed.

Tags: , ,

About

Erik is the owner of online media company Bernskiold Media that runs tutorial website Bernskiold learning and he is a Web Designer, Photographer and Software-Trainer.

Leave a Reply

Your email address will not be published. Required fields are marked *

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>