(+84) 463.28.7979

How to use Custom Post Types in WordPress 3.0

Since version 2.9, WordPress has introduced the ability to use custom post types. Now with version 3.0, things are taken a bit further with the option of creating panels for your custom post types. In this tutorial, we will show you how to implement Custom Post types in your site into your WordPress site.

Before we begin make sure you are running on WordPress 3.0 (Beta versions for now).

Watch the Screencast

Creating Custom Post Types – Using Plugins

As of version 3.0, WordPress does not have any built-in UI (user-interface) to create custom post types. There are only two options that we can use to create custom post types: plugins or to hard code them into your theme’s functions.php file. First, lets look over how we can use plugins to create custom post types.

Custom Post Type UI

Custom Post Types UI

Custom Post Type UI is a plugin developed by Brad Williams of WebDevStudios which allows you to easily create custom post types and taxonomies. One of the coolest feature of this plugin is that it generates a code to create custom post types, so you can then paste it into your theme’s functions.php file. One of the quirks of this plugin is the inability to share taxonomies among all your post_types.

Generate code for Custom Posts Types

From the Custom Post Type UI panel click on “Add New”.

Add New Button for Custom Post Type UI

Next you are given a few options to fill in. The “Post Type Name” is what will be used by WordPress to query all the posts from such post_type. The “Label” is what will be displayed on the sidebar of your Dashboard, just like the regular “Post” menu is. If you expand the “View Advanced Options” you’ll see a few more options that you can configure. Most are self explanatory, such as “Public” and “Show UI”. The first one when set to true allows the custom post type menu to be displayed on the sidebar, and the other one (show ui) when set to true generates the menu panel.

“Rewrite” is what allows the custom post type to use SEO Friendly WordPress URLs (Permalinks). The “Custom Rewrite Slug” can be set to anything that you like. WordPress will use this slug to generate the permalinks. So if we have example.com with a custom rewrite slug of “movies” your custom post type permalink would look like example.com/movies

WordPress “Query Var” function allows you to query your custom post type’s post. So if we used the example given earlier, to access a post with the tittle, My First Movie Post, which is written under the Movies post_type, we can enter example.com/?movies=my-first-movie-post. So the query variable looks like this: ?posttypename

Finally you can choose the different features supported by your custom post type, such as thumbnails/featured image, and excerpts.

Custom Post Types UI Options

CMS Press


CMS Press is another plugin that allows you to add custom post types and taxonomies, developed by Michael Pretty. One of the best feature of this plugin is the ability to share taxonomies between all the post_types.

To create a custom post type with CMS Press is pretty straight forward, just like we did with Custom Post Type UI. The two major differences is the “Content Type Identifier” and the “Permalink Structure”. The “Content Type Identifier” is simply the slug name you want to give to your custom post type. The “Content Type Identifier” then gives %identifier% the value of the slug name you have chosen to create the permalink. This feature allows you to set a different permalink structure for each of your custom post types.

CMSPress Options

So if you want your custom post type permalink to look like example.com/category/post_type/postname You’ll enter the following under the “Content Type Identifier”: %category%/%identifier%/%postname%

Creating Custom Post Types – Using Functions.php file

Hard Code Custom Post Types

If you prefer to use custom post types without a plugin, then just add the following code to your theme’s functions.php file:

// Creates Movies post type
register_post_type(‘movies’, array(
‘label’ => ‘Movies’,
‘public’ => true,
‘show_ui’ => true,
‘capability_type’ => ‘post’,
‘hierarchical’ => false,
‘rewrite’ => array(‘slug’ => ‘movies’),
‘query_var’ => true,
‘supports’ => array(
) );

Lets dissect the code.

register_post_type( $post_type, $args ): This function accepts two parameters, $post_type or the name of the post type, and $args, an array of arguments.

label: Plural name given to the post type which is displayed in the admin panel sidebar.

public: true/false. Allows the admin UI to be populated with posts of this type.

show_ui: true/false. Shows or hides a default UI to mange this post type.

capability_type: Default: post Post type to use for checking read, edit, and delete capabilities.

hierarchical: Whether the post type is hierarchical.

rewrite: true/false. Default: true If slug argument is entered then the slug name is prepended to the posts.

query_var: true/false Sets the post type name as a query variable.

supports: Default: title and author Sets different support features the post type allows.

Visit the WordPress Codex for more info on register_post_type().

Displaying Custom Post Type Posts

To display the posts from your custom post type, add the following codes in the loop. Replace “name” with the name of your post type. Note: You don’t have to add the custom post types in your index.php file. You can create a Custom WordPress page and run the following query within the Loop.

query_posts( ‘post_type=name’);

To display posts from more than one post type, change the above code to the following. Change movies with your custom post type name.

query_posts(array(‘post_type’ => array(‘post’, ‘movies’)));

The above code will display all the post from the regular post type (post) and from the custom post type, movies.

That’s it. We hope this tutorial has been helpful and don’t forget to post any questions in the comments.

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>