WordPress Custom Posts

WordPress 3.0 has introduced the ability to create a Custom Post type. Custom Post types allow you to generate a post which can collect more than just a title and block of text. This example is used for a cat breeder and allow them to list (blog style) the cats theat they have available. By having each of the data fields stored individually you can then, through your theme files, create a custom layout for the content.

An example of a site using an extended version of this post type is www.obengals.com

To create a custom post you will make changes to the functions.php file of your theme.

Register a custom post and set up actions

add_action('init', 'inov8_custompost_register');
add_action("admin_init", "admin_init");
add_action('save_post', 'inov8_save_details');
 
function inov8_custompost_register() {
 Custom post for a Cat (feline)
	$labels = array(
		'name' => _x('Cats', 'post type general name'),
		'singular_name' => _x('Cat', 'post type singular name'),
		'add_new' => _x('Add New', 'Cat item'),
		'add_new_item' => __('Add New Cat Item'),
		'edit_item' => __('Edit Cat Item'),
		'new_item' => __('New Cat Item'),
		'view_item' => __('View Cat Item'),
		'search_items' => __('Search Cats'),
		'not_found' =>  __('Nothing found'),
		'not_found_in_trash' => __('Nothing found in Trash'),
		'parent_item_colon' => ''
	);
 
	$args = array(
		'labels' => $labels,
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true,
		'_builtin' => false,
		'query_var' => true,
		'menu_icon' => get_stylesheet_directory_uri() . '/icon_cats.png',
		'rewrite' => true,
		'capability_type' => 'post',
		'hierarchical' => false,
		'menu_position' => null,
		'rewrite' => array("slug" => "cats","with_front" => false), // Permalinks format
		'supports' => array('title','editor','thumbnail','page-attributes')
	  ); 
 
	register_post_type( 'inov8_cats' , $args );

Add a meta box . This is a space on the admin post page where you can setup user controls (text box’s, lists etc) to collect the extra data for the post. This can be aligned either under the main text input area or in the sidebar area with categories.

function admin_init(){
add_meta_box("cat-meta", "Cat Details", "inov8_cat_meta", "inov8_cat", "normal", "low");
}

Function to add meta data fields in the cat meta box.

function inv8_cat_meta(){
  global $post;
  $custom = get_post_custom($post->ID);
  $catename = $custom["catname"][0];
  $catesex = $custom["catsex"][0];
  $price = $custom["price"][0];
  $purchasestatus = $custom["purchasestatus"][0];
  $bday = $custom["bday"][0];
  $bmonth = $custom["bmonth"][0];
  $byear = $custom["byear"][0];
  $issold = "";
  $isforsale="";
  $iscontract="";
  $malechecked = "";
  $femalechecked = "";
  if ($catesex =="male"){
	$malechecked = "checked=checked";  
  }else{
	  $femalechecked = "checked=checked"; 
  }
  switch ($purchasestatus){
	
	case "sold":
		$issold = "selected='selected'";
	break;  
	case "forsale":
		$isforsale = "selected='selected'";
	break;  
	case "contract":
		$iscontract = "selected='selected'";
	break;  
	  
  }?>
  <p><label><p><label>Name:</label><br/>
<input name="catename" value="<?php echo $catename; ?>" /></p>
<p><label>Price:</label><br/>
<input name="price" value="<?php echo $price; ?>" /></p>
<p> <label>Purchase Status:</label><br/> <select name="purchasestatus" id="purchasestatus">
<option value="">Select An Option</option>
<option value="sold" <?php echo $issold;?>>Sold</option>
<option value="forsale" <?php echo $isforsale;?>>Forsale</option> <option value="contract" <?php echo $iscontract;?>>Under Contract</option> </select> </p>
<p> <label>Birthdate:</label> <input name="bday" value="<?php echo $bday; ?>" size="3"/> Day - <input name="bmonth" value="<?php echo $bmonth; ?>" size="3"/> Month - <input name="byear" value="<?php echo $byear; ?>" size="5"/> Year </p>
<p> <input name="catsex" type="radio" value="male" <?php echo $malechecked; ?>/> Mal
<input name="catsex" type="radio" value="female" <?php echo $femalechecked; ?>/> Female </p> <?php}

Function to save the post.

function inov8_save_details($post_id){
  global $post;
 if (!defined('DOING_AUTOSAVE') && !DOING_AUTOSAVE){ 
        return $post_id;
 }
  if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename(__FILE__) )) {
    return $post_id;
  } 
 
    if ( !current_user_can( 'edit_page', $post_id ) )
      return $post_id;
 
    if ( !current_user_can( 'edit_post', $post_id ) )
      return $post_id;

 
  switch($_POST['post_type']){
	case "inv8_cats":  
	  	update_post_meta($post->ID, "catname", $_POST["prodigeename"]);
  		update_post_meta($post->ID, "catsex", $_POST["prodigeesex"]);
		update_post_meta($post->ID, "price", $_POST["price"]);
		update_post_meta($post->ID, "bday", $_POST["bday"]);
		update_post_meta($post->ID, "bmonth", $_POST["bmonth"]);
		update_post_meta($post->ID, "byear", $_POST["byear"]);
		update_post_meta($post->ID, "bdate", mktime(0,0,0,$_POST["bmonth"],$_POST["bday"],$_POST["byear"]));
		update_post_meta($post->ID, "purchasestatus", $_POST["purchasestatus"]);
  		}
		break;
   }
  return $post_id;
}