Custom Post Types in WordPress

Glenn Glenn's Blog, Slick Media Blog 0 Comments

What are Custom Post Types?

Custom Post Post Types or Custom Posts are a great innovation in WordPress that extend the standard functionality of ‘posts’ beyond that of the normal blogging sphere to make WordPress more of a fully fledged Content Management System (CMS). For example in the past where you may have used a custom query (such as page of posts)  to filter posts by say a particular category you can now define a specific custom post, which will have its own menu item in the WordPress Dashboard like so:-

Custom Post Types in WordPress Custom Post Types in WordPress screen capture 1

So with Custom Posts what we gain is a higher level of specific functionality for a certain ‘Post Type’ – in our example we have ‘Restaurants’ as our Custom Post Type.

What are Custom Post Types Used For?

As above Custom Post Types are very useful where you want to separate off the normal post functionality of your WordPress Blog and make it a full CMS. Custom Post Types are often used for the following forms of functionality:-

  • Portfolios
  • Directory Listings
  • News Items (as opposed to Blog items)
  • Lists
  • Custom Systems

We use Custom Post Types extensively in WordPress to give our clients much greater functionality and control of their site(s) content.

How are Custom Post Types Added to WordPress?

Custom Post Types are typically added directly to the function.php file of your WordPress theme and using our example of Restaurants this would be added as so:-

Custom Post Type Example (Restaurants)


// registration code for restaurants post type
	function register_restaurants_posttype() {
		$labels = array(
			'name' 				=> _x( 'restaurants', 'post type general name' ),
			'singular_name'		=> _x( 'restaurant', 'post type singular name' ),
			'add_new' 			=> _x( 'Add New', 'restaurant'),
			'add_new_item' 		=> __( 'Add New restaurant '),
			'edit_item' 		=> __( 'Edit restaurant '),
			'new_item' 			=> __( 'New restaurant '),
			'view_item' 		=> __( 'View restaurant '),
			'search_items' 		=> __( 'Search restaurants '),
			'not_found' 		=>  __( 'No restaurant found' ),
			'not_found_in_trash'=> __( 'No restaurants found in Trash' ),
			'parent_item_colon' => ''
		);

		$taxonomies = array();

		$supports = array('title','editor','author','thumbnail','excerpt','custom-fields','comments','revisions');

		$post_type_args = array(
			'labels' 			=> $labels,
			'singular_label' 	=> __('restaurant'),
			'public' 			=> true,
			'show_ui' 			=> true,
			'publicly_queryable'=> true,
			'query_var'			=> true,
			'capability_type' 	=> 'post',
			'has_archive' 		=> true,
			'hierarchical' 		=> false,
			'rewrite' 			=> array('slug' => 'restaurants', 'with_front' => false ),
			'supports' 			=> $supports,
			'menu_position' 	=> 5,
			'menu_icon' 		=> 'https://www.slickmedia.co.uk/wp-content/plugins/easy-content-types/includes/images/icon.png',
			'taxonomies'		=> $taxonomies
		 );
		 register_post_type('restaurants',$post_type_args);
	}
	add_action('init', 'register_restaurants_posttype');

Taxonomies (To split Restaurants by ‘Location’)



// registration code for locations taxonomy
function register_locations_tax() {
	$labels = array(
		'name' 					=> _x( 'Locations', 'taxonomy general name' ),
		'singular_name' 		=> _x( 'Location', 'taxonomy singular name' ),
		'add_new' 				=> _x( 'Add New Location', 'Location'),
		'add_new_item' 			=> __( 'Add New Location' ),
		'edit_item' 			=> __( 'Edit Location' ),
		'new_item' 				=> __( 'New Location' ),
		'view_item' 			=> __( 'View Location' ),
		'search_items' 			=> __( 'Search Locations' ),
		'not_found' 			=> __( 'No Location found' ),
		'not_found_in_trash' 	=> __( 'No Location found in Trash' ),
	);

	$pages = array('restaurants');

	$args = array(
		'labels' 			=> $labels,
		'singular_label' 	=> __('Location'),
		'public' 			=> true,
		'show_ui' 			=> true,
		'hierarchical' 		=> false,
		'show_tagcloud' 	=> false,
		'show_in_nav_menus' => true,
		'rewrite' 			=> array('slug' => 'locations'),
	 );
	register_taxonomy('locations', $pages, $args);
}
add_action('init', 'register_locations_tax');

Plugins that create Custom Posts Types in WordPress

If you are new to Custom Post Types and are looking for software/a plugin to add Custom Post Types to your WordPress Site then we recommend one of two plugins. Don’t waste your time looking for alternatives and choose on of the below.

1. Custom Press by WPMUdev

This is a great premium plugin covering all the bases including the creation of Custom Post Types, Taxonomies and Meta Boxes and these are all handled within a single tabbed admin area:-

Custom Post Types in WordPress Custom Post Types in WordPress screen capture 11

This plugin is quick and simp to use and can be purchases on its own for a very reasonable $17 or alternatively you can signup to WPMUdev and get a whole host of other great plugins, great support and regular updates.

Get the plugin from WPMUdev and/or benefit form loads of other great Premium WordPress plugins

Easy Content Types for WordPress by Pippin Williamson on code canyon.net

This is a very comprehensive plugin that also caters to Custom Post Types, Taxonomies and Meta Boxes and is justifiably a best selling item on code canyon.net. The plugin is created by the author Pippin Williamson and he is a very well established premium WordPress plugin author offering a range of other plugins also on CodeCanyon. This one is the most popular and intuitively breaks down the process of creating advanced Custom Posts:-

Custom Post Types in WordPress Custom Post Types in WordPress screen capture 2

What makes this Plugin a stand-out Custom Post Type plugin is the ability to export not only the Post Types, but also the related Taxonomies and Metaboxes. This makes embedding the Custom Post Types directly to your theme as simple as cut and paste.

Pippin provides great support for the plugin and also regular updates to codecanyon.net.

Get the plugin from code canyon.net and browse loads of other great premium plugins to enhance your WordPress site

About the Author
Custom Post Types in WordPress Custom Post Types in WordPress 0da77002febbfe28d15144a57bbcc2c9 s 180 d mm r g

Glenn

A highly experienced WordPress Web Developer, Front-end and Back-end Developer & New Media Specialist with extensive knowledge of a wide spectrum of technologies in the Development and Creative Industries built up over a number of years.

Leave a Reply

Your email address will not be published. Required fields are marked *