Exactly what makes WordPress a powerful CMS? What are the features that make it more than just a blogging software? The most powerful feature of WordPress is that it is highly customizable. In this post and a few upcoming posts, I will be discussing these features with you. So if you are not already an expert level WordPress user you might want to follow @WPlift to learn about these features.

Custom Post Types

Custom Post Types is the first WordPress feature that I would like to discuss. It comes under advanced topics so a lot of beginner level WordPress users are unaware of how it works and what they can do with custom post types, follow our WordPress Custom Post Types Tutorial to learn more.

As a CMS WordPress can publish different types of content and it is flexible enough for website owners to define their own content types. Custom Post Types is a feature of WordPress that allows you to create those post types to publish different kind of content. By default WordPress comes with a number of default post types :

Post (Post Type: ‘post’)

Page (Post Type: ‘page’)

Attachment (Post Type: ‘attachment’)

Revision (Post Type: ‘revision’)

Navigation menu (Post Type: ‘nav_menu_item’)

You are not limited to using just those though, you are able to create a post type for any kind of content which you need to fit your website’s need.

For example, the most popular type of post type that a lot of themes use is “portfolio” so you can add images or photos separately to your blog posts. Other types of post types might be listings for a real estate site, products for a eCommerce site, reviews, clients, staff and so on.

In this tutorial we will be assuming you want to create a website for displaying movie reviews and creating a post type named “Movie Reviews”. Here is the code which you need to add in your theme’s functions.php file.

[php]add_action( ‘init’, ‘create_post_type’ );

function create_post_type() {

register_post_type( ‘movie_reviews’,

array(

‘labels’ => array(

‘name’ => __( ‘Movie Reviews’ ),

‘singular_name’ => __( ‘Movie Review’ )

),

‘public’ => true,

‘has_archive’ => true,

‘menu_position’=>5,

)

);

}[/php]

Tip: Add this code at the bottom if your functions.php already has code in it.

Let me explain this code to you. Register Post Type function has two parameters the first one is post type where you define what you would like to call your custom post type, you need to give it a unique name. The second parameter is an array of arguments. The first argument is labels which again is an array of arguments to describe the labels to be used for this post type. Other arguments describe different settings for this custom post type. Look here for available arguments that can be used.

Back to our Custom Post type, now as soon as you update your functions.php a new menu item “Movie Reviews” will appear in the admin menu on your left. Taking your mouse over, this menu will display a submenu with two items “Movie Reviews” and “Add New”. Clicking on Add New will take you to post editor where you can create a new post as Movie Reviews post type.

Now you must be thinking that whats the point of this? Wouldn’t it be easier to just create a post and file it under “Movie Reviews” category?

The point is that we have just created a post type, now we are going to learn how we can do other things with this post type so that it becomes really useful for us. Right now it is just a post and in the next steps we will customize how this particular type of post is handled by WordPress on your site.

Firstly, we want to change some labels so that when users are creating, editing or searching for the movie reviews inside the admin section they don’t get confused and are able to distinguish movie reviews from other type of posts. To do that we add more labels into our register_post_type function. Our final function would look like this:

[php]function create_post_type() {

register_post_type( ‘movie_reviews’,

array(

‘labels’ => array(

‘name’ => __( ‘Movie Reviews’ ),

‘singular_name’ => __( ‘Movie Review’ ),

‘add_new_item’ => __(‘Add New Movie Review’),

‘add_new’ => _x(‘Add New’, ‘movie review’),

‘edit_item’ => __(‘Edit Movie Review’),

‘new_item’ => __(‘New Movie Review’),

‘all_items’ => __(‘All Movie Reviews’),

‘view_item’ => __(‘View Movie Reviews’),

‘search_items’ => __(‘Search Movie Reviews’),

‘not_found’ => __(‘No Movie Reviews found’),

‘not_found_in_trash’ => __(‘No Movie Reviews found in Trash’),

‘parent_item_colon’ => ”,

‘menu_name’ => ‘Movie Reviews’

),

‘public’ => true,

‘has_archive’ => true,

‘menu_position’=>5

)

);

}

[/php]

Edit Screen for your Custom Post Types

When creating a new post in your newly created custom post type you would notice that it is a little different from your default posts and pages. You will have just a Title, Post Editor and Publish options on the screen. By default custom posts are created with these options only. Now we will need to add other options in the Edit Screen. To do that we go back to our register_post_type function and add a new array to it just after menu_position=>5, (Tip: Add a comma after 5 as it is no longer the last item in the array):

'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields', 'comments' )

Now that we have created a post type, the next thing we would like to do is to make sure that Movie Review posts are shown differently on your website, your final code should look like this :

[php]

add_action( ‘init’, ‘create_post_type’ );

function create_post_type() {

register_post_type( ‘movie_reviews’,

array(

‘labels’ => array(

‘name’ => __( ‘Movie Reviews’ ),

‘singular_name’ => __( ‘Movie Review’ ),

‘add_new_item’ => __(‘Add New Movie Review’),

‘add_new’ => _x(‘Add New’, ‘movie review’),

‘edit_item’ => __(‘Edit Movie Review’),

‘new_item’ => __(‘New Movie Review’),

‘all_items’ => __(‘All Movie Reviews’),

‘view_item’ => __(‘View Movie Reviews’),

‘search_items’ => __(‘Search Movie Reviews’),

‘not_found’ => __(‘No Movie Reviews found’),

‘not_found_in_trash’ => __(‘No Movie Reviews found in Trash’),

‘parent_item_colon’ => ”,

‘menu_name’ => ‘Movie Reviews’

),

‘public’ => true,

‘has_archive’ => true,

‘menu_position’=>5,

‘supports’ => array( ‘title’, ‘editor’, ‘thumbnail’, ‘custom-fields’, ‘comments’ )

)

);

}

[/php]

Templating your Custom Post Type

Now we have created the post type, we need to display it within your WordPress theme.

The WordPress template engine will recognize templates for your custom post type in the format single-posttype.php and archive-posttype.php format. For example we have created a custom post type “movie_reviews” so will name our single post template “single-movie_reviews.php” and archive post template as “archive-movie_reviews.php”.

Displaying Your Custom Post Types

You may wonder why the content you have created under the new post type is nowhere to be seen on your website yet, we have told WordPress to create the post type and store the content but we haven’t told it how to display that content yet, so we need to create some extra templates in our theme to do so.

Now we create a template for our theme to display the Movie Reviews custom post type.

Copy the code of page.php if your theme has one, or index.php from your themes folder, paste this code into a new text file and save it as “archive-movie_reviews.php”.

Replace the meta information of the template with this code:

[php]<?php

/**

* Template Name: Movie Reviews Template

*/[/php]

After that place this code just before the WordPress Loop:

[php]<?php query_posts(array(‘post_type’=>’movie_reviews’)); ?>[/php]

Save the file and exit. Copy and paste this file into your themes folder if you haven’t already saved it there.

Go to Pages and create a new page, give it a title Movie Reviews, you don’t need to write anything in the editor. From the Page Attributes meta box select Template > Movie Reviews and publish the page.

Now if you are using WordPress default menu and it is automatically displaying your pages then you will have Movie Reviews page appear in the menu. If it is not then go to Menus and add the page Movie Reviews to your navigation.

Create Post Types with a Plugin

If you would prefer to not to edit code and template files from your theme, then there is a plugin called “Custom Post Type UI” which provides a visual interface for creating post types, you can download this for free.

Conclusion

In this tutorial we have created a custom post type which allows the admins and authors of the website to add different type of content. You can create custom post types for many types of contents like recipes, book reviews, product listings, short news items, classifieds and so on.

In the next few posts I will be discussing other WordPress customization options such as Taxonomies, Post Formats, and Meta Boxes.