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'); 
  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'), 
  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 = "
      {$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(

    $post_type_args = array(
    register_post_type((string)$post_type->post_name, $post_type_args);

function set_custom_post_types($query)
  global $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


Leave a Reply

On The Web


Recent Comments