• Blog

    Latest from our Blog

  • Tools

    Our Favourite WP Tools

  • Hosting

    Recommended Web Hosts

  • Coupons

    Get great money off deals

  • Themes

    WordPress Theme Directory

  • Plugins

    WordPress Plugins Directory

  • Promote

    Your WordPress Product

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 advance topics so a lot of beginner level WordPress users are unaware of how it works and what they can do with custom post types.

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 post, page, attachment, revision, and nav menu post types.

Now let’s assume that you want to add another post type, you want to call it Movie Reviews. Here is the code which you need to add in your theme’s functions.php file.

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,
		)
	);
}

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:

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
		)
	);
}

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.

Templating your Custom Post Type

WordPress templating engine would 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

If you are new to WordPress and Custom Post types and you have followed this tutorial you must be panicked by now as the posts you create in your Custom Post Types are nowhere to be seen on your website yet. The reason for this is that the main purpose of creating Custom Post Types is to create content other than posts. Now we create a template for Movie Reviews custom post type.
Copy the code of page.php from your themes folder, paste this code into a new text file and save it as “archive-moviereviews.php”.
Replace the meta information of the template with this code:

<?php
/**
 * Template Name: Movie Reviews Template
 */

After that place this code just before the WordPress Loop:

<?php query_posts(array('post_type'=>'movie_reviews')); ?>

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.

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 what not. At WPLift we have covered Custom Post Types in the past, for example check out Oliver’s list of Custom Post Types Tutorials and Tools, or my post about plugins to create and manage custom post types, fields, and taxonomies. In the next few posts I will be discussing other WordPress customization options such as Taxonomies, Post Formats, and Meta Boxes.


Noumaan is a blogger and social media expert. He loves Quora, Facebook, Wordpress, OpenSource Software and The Sims.

Leave Yours +

7 Comments

  1. Simply and Clearly explained, I will use custom posts for “coupons” section in my blog.

  2. Anonymous

    Thanks @150547ea3ad411443417612800349b67:disqus  I am glad you liked it. Good look with your coupons section. 

  3. Thanks for that walk through. It’s seems easy to do, might as well apply it to my blog.

  4. Littered with errors. The post type you define at the beginning has an underscore and the one you call at the end does not…. how would this work?

  5. Sachin Kumar

    this tutorial is too good i want more read as like this tutorial

  6. My

    Great tutorial!

  • Comments are Closed

Search

Our Sponsors