So what is all of the buzz about custom post types in WordPress 2.9 and 3.0? What can we do with a “post_type” and why does it matter? For some of us developers out there who have known the secret that WordPress is actually much more than just a blogging platform this in no big news. To the rest of the developers out there this translates to “WordPress is now a CMS” or something to that effect.

For some time now WordPress has been more than just a blogging tool. With a little bit of imagination WordPress can be used not only as a CMS (Content Management System) but also as a PHP framework on which to launch complex web applications. By simply allowing for developers to register any custom “post_type” WordPress has made this task a whole lot easier for us.

This blog post will be a work in progress, WordPress 3.0 is still in development at this time and not due to be launched for another couple of months. As new options become available over the next couple of months I will continue to update this post to reflect the changes being made in the nightly builds.

  • How do you plan on using custom post types in your WordPress themes and plugins?
  • Will this change the way you currently use categories and tags to differentiate content types?
  • Does this help solidify in your mind that WordPress is finally a CMS?

Also, make sure to check out what Justin Tadlock has to say on this matter.

$custom_post_types = array();

add_action('init', 'init_custom_post_types'); 
if(!is_admin())
  add_filter('pre_get_posts', 'set_custom_post_types');

function init_custom_post_types()
{
  global $wpdb, $custom_post_types;

  // Create a default custom 'post_type' called custom_post_types.
  // This adds a "Post Types" UI panel in the admin and allows us
  // to create new post types on the fly just by adding a new post
  // through the admin interface.
  $post_type_args = array(
    'label'=>__('Post Types'), 
    'public'=>true,
    'show_ui'=>true,
    'exclude_from_search'=>true
  );
  register_post_type('custom_post_types', $post_type_args);

  // If there are any posts with the 'post_type' of custom_post_types
  // their names and titles will be returned and new 'post_types' will
  // automatically be registered for each record.
  $sql = "
    SELECT
      {$wpdb->posts}.post_name,
      {$wpdb->posts}.post_title
  	FROM 
      {$wpdb->posts}
  	WHERE
      {$wpdb->posts}.post_type = 'custom_post_types'
      AND {$wpdb->posts}.post_status = 'publish'";

  foreach($post_types = $wpdb->get_results($sql) as $post_type)
  {
    // Store a global array of custom post types for modifying the
    // search query to include the new post types we just registered
    $custom_post_types[$post_type->post_name] = array(
      'name'=>$post_type->post_name,
      'title'=>$post_type->post_title
    );

    $post_type_args = array(
      'taxonomies'=>array('post_tag','category'), 
      'label'=>__((string)$post_type->post_title), 
      'public'=>true, 
      'show_ui'=>true, 
      'exclude_from_search'=>false,
      'register_meta_box_cb'=>'init_custom_meta_boxes',
      '_builtin'=>true,
      'supports'=>array('title','editor','thumbnail','excerpts','trackbacks','custom-fields','comments','author','revisions')
    );
    register_post_type((string)$post_type->post_name, $post_type_args);
  }
}

function set_custom_post_types($query)
{
  global $custom_post_types;

  if($custom_post_types)
  {
    // Here we create an array of all valid post types
    // that we want to appear on the blog.
    $post_types = array('post', 'attachment'); //'page', 
    foreach($custom_post_types as $key=>$post_type)
    {
      $post_types[] = $post_type['name'];
    }
  }
  
  // Now we modify the query to include the newly registed post types
	$query->set('post_type', $post_types);
	return $query;
}

function init_custom_meta_boxes($args)
{
  add_meta_box('setcustommeta', __('Attributes'), 'set_custom_meta_boxes', $args->post_type, 'normal', 'core');
}

function set_custom_meta_boxes($post)
{
  // add custom meta boxes
}

Comments

Leave a Reply

On The Web

Categories