How to create a custom post type in WordPress

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;
    }
}
Free Graphics, Icons, Tutorials
Free Graphics Free Christmas Vector Icon Graphics Pack 2017Free Fitness Vector Icons Graphics PackFree Camping Vector Graphics PackFree Summer Graphics PackFree File Icon PackFree Fast Food Vector Graphics
Sharing is caring...
Like & Follow
Share the love, share this page! Facebook Twitter Digg Reddit LinkedIn Pinterest Email
Close [X]
The Web Taylor
1000 Lakeside North Harbour Portsmouth, Hampshire PO6 3EN
02392 123358