There are two ways to implement custom post types in your WordPress site, which I know. First is to add the snippet into your active theme’s functions.php file, other is to add snippet to a functions-specific plugin. I prefer and recommend functions-specific plugin way, because it keeps the theme separate and undisturbed from custom functionality. Unless you want to allow your theme to mess with your custom functions, then you will probably put the codes within theme functions file.

However, you will learn about how to create and use WordPress custom post types today. Custom post type means making a whole separate section for posting similar to Post/Page and here is the screenshot of my own custom post types.

You may also like: Brief Introduction about Custom Post Types

How to create custom post type (cpt) in WordPress

Before I begin to tell you about custom post type, let me tell you that template tag register_post_type() was introduced in WordPress 2.9 and we’re going to use it to register our post type. As I said earlier we have to put some sort of codes within theme or separate plugin file to create custom post type in order to get the basic functionality up. Here, you just need the name for post type, two labels and whether post type will be public.

Following is the least code you need to create post type:

add_action('init', 'st_portfolio_type');
function st_portfolio_type() {
 register_post_type('hs_portfolio',
 array(
 'labels' => array(
 'name' => __('Portfolio'),
 ),
 'public' => true,
 )
);
}

So.. just copy and paste this code in your functions.php file, which I don’t recommend. Or paste into a functions-specific plugin. It will work. To explain how it works is that; I defined a function called st_portfolio_type (prefixed it with st_, so it has a custom taste). This function calls another function register_post_type(), which requires one argument called $post_type, it is the name of post type. Another is $args, which is a list of arguments and is an array.

Okay.. now the details!

Till now.. we’ve discussed what custom post type(s) is/are, what are its uses, ideas to get most out of them and many things. Also we discussed minimum code required to build a custom post type. Now it’s time to delve into the details. We’re going to learn about many different arguments that register_post_type() function can take and what each argument does.

labels (array)

First argument ‘labels’ is an array of strings defining many texts which will be used in WordPress dashboard back-end. Here’s a quick list of array elements:

‘name’: Plural form of your post type name. Will be displayed on ‘All items‘ page.
‘singular_name’: Singular form of your post type name. Default: ‘name’ value
‘menu_name’: Name of post type’s menu. Defaults to ‘name’ value
‘add_new’: The add new text. Default is ‘Add New’
‘add_new_item’: The add new item text.
‘edit_item’: The edit item text
‘new_item’: Text for new item. Like ‘New Post’, ‘New Page’?
‘all_items’: The all items’ text used in the menu.
‘name_admin_bar’: Name for Add New in dropdown of the admin bar
‘view_item’: Text for view item. Like View Post, View Page, etc.
‘not_found’: Text for not found. What text to display when no items are found? Default is No posts found/No pages found
‘not_found_trash’: Text for not found for trash. Default is No posts/pages found in trash
‘parent_item_colon’: The parent text.
‘search_items’: Text for search button. Default is Search posts/pages

‘labels’ is an array of arguments. These arguments define elements within WordPress UI. Let’s define a variable called $labels and assign that variable this array.

$labels = array(
'name'            => __('Portfolio'),
'singular_name'   => __('Portfolio Item'),
'menu_name'       => __('Portfolio'),
'add_new'         => __('Add New'),
'add_new_item'    => __('Add New Portfolio Item'),
'edit_item'       => __('Edit Item'),
'new_item'        => __('New Item'),
'all_items'       => __('All Items'),
'name_admin_bar'  => __('Add New Item'),
'view_item'       => __('View Item'),
'not_found'       => __('No Item Found'),
'not_found_trash'=> __('No Item Found in Trash'),
'search_items'    => __('Search Items')
);

Supports

This array of arguments defines what features your CPT is going to support.

However, if you are new to custom post types then I would highly recommend you to use WordPress plugin for that to start using custom post types in WordPress. It is way out for you trust me and you can still learn to use CPTs don’t worry.

Best plugins so far for CPTs:

I’m sure, after reading this article you will be able to take a start with custom post types in WordPress.