What is a Custom Post Type? The WordPress Codex defines a Custom Post Type as “a Post Type you can define.”  Ok maybe not the best definition, so lets look at what is a custom post type and how to create one to display within your WordPress Dashboard.

A post type can be defined as a type of content, a classification.  Within WordPress, post types are stored within column in the wp_posts table called post_type.  There are 5 default types: Post, Page, Attachment, Revisions and Nav Menus.  If you need to classify your content in a different then you can set up a Custom Post Type.

There are several ways you can go about setting up your custom post type. First you can set up your custom post type directly in your theme – you will need to put the code below into your theme’s functions.php code, replace “Products” with the name that you would want to give it.  Alternatively, there are various plugins in WordPress plugin repository to setup a custom post type.  One that I can recommend by experience is Custom Post Type UI by Brad Williams.

add_action( 'init', 'register_cpt_product' );

function register_cpt_product() {

$labels = array(
'name' => _x( 'Products', 'product' ),
'singular_name' => _x( 'Product', 'product' ),
'add_new' => _x( 'Add New', 'product' ),
'add_new_item' => _x( 'Add New Product', 'product' ),
'edit_item' => _x( 'Edit Product', 'product' ),
'new_item' => _x( 'New Product', 'product' ),
'view_item' => _x( 'View Product', 'product' ),
'search_items' => _x( 'Search Products', 'product' ),
'not_found' => _x( 'No products found', 'product' ),
'not_found_in_trash' => _x( 'No products found in Trash', 'product' ),
'parent_item_colon' => _x( 'Parent Product:', 'product' ),
'menu_name' => _x( 'Products', 'product' ),
);

$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'This is the main product custom post type.',
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'custom-fields', 'post-formats' ),
'taxonomies' => array( 'category', 'post_tag', 'page-category', 'brand', 'size', 'color' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 20,

'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => 'product',
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post'
);

register_post_type( 'product', $args );
}

Code Analysis

First, you will need to setup an action hook, initialize and set your register post type function. Next you will be defining and “labeling” your custom post type with the $labels and $args array variables.  These variables will tell you name, description, and the overall function of the custom post type.  A great resource on defining each segment of these arrays are found in the WordPress Codex.

Additional Resources:

WordPress Codex section on Custom Post Types
Custom Post Type Generator

Enhanced by Zemanta