How to create a custom post type in WordPress

14th December 2012

WordPress custom post types are very handy, if you want an extra section on your blog that is about a different subject or contains different content then this is for you. So lets say you have a business website that contains information about your business, and you want to show the products that you offer in a separate section of the site. So all posts contained within the “Products” custom post type will be found under http://domain.com/products/example

Creating a custom post type in Wordpres is very easy. Just replace the word “products” with your desired custom post type name and copy & paste into your functions.php file…

add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'products',
		array(
			'labels' => array(
			'name' => __( 'Products' ),
			'singular_name' => __( 'Products' ),
			),
		'menu_position' => 4,
		'rewrite' => array('slug'=>'','with_front'=>false),
		'public' => true,
		'has_archive' => true,
		'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail' ),
		)
	);
}

This code enables support for: excerpt, custom-fields, thumbnails

If you want to have multiple post types your could should look something like this…

function create_post_type() {
register_post_type( 'products',
		array(
			'labels' => array(
			'name' => __( 'Products' ),
			'singular_name' => __( 'Products' ),
			),
		'menu_position' => 4,
		'rewrite' => array('slug'=>'','with_front'=>false),
		'public' => true,
		'has_archive' => true,
		'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail' ),
		)
	);
	register_post_type( 'testimonials',
		array(
			'labels' => array(
			'name' => __( 'Testimonials' ),
			'singular_name' => __( 'Testimonials' ),
			),
		'menu_position' => 5,
		'rewrite' => array('slug'=>'','with_front'=>false),
		'public' => true,
		'has_archive' => true,
		'supports' => array( 'title', 'editor', 'comments', 'excerpt', 'custom-fields', 'thumbnail' ),
		)
	);
}

Also if you wanted to add support for Tags and Categories, add the following code below…

add_action('init', 'add_default_boxes');
function add_default_boxes() {
    register_taxonomy_for_object_type('category', 'products');
    register_taxonomy_for_object_type('category', 'testimonials');
    register_taxonomy_for_object_type('post_tag', 'products');
    register_taxonomy_for_object_type('post_tag', 'testimonials');
}
function my_post_types( $post_types ) {
    $post_types[] = 'products';
    $post_types[] = 'testimonials';
    return $post_types;
}
add_filter( 'cpt_post_types', 'my_post_types' );
add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if(is_category() || is_tag()) {
    $post_type = get_query_var('post_type');
	if($post_type)
	    $post_type = $post_type;
	else
	    $post_type = array('products', 'testimonials');
    $query->set('post_type',$post_type);
	return $query;
    }
}

Last updated by at .

Comments...


Facebook Application Directory
3rd January 2013

Good information about custom code.

Got An Opinion On This Article?

Share your comments with us...


*