Using Custom Post Types in Wordpress 3.0

Posted: February 23rd, 2010 | Author: David Scott Tufts | Filed under: PHP, Web Development, Wordpress | Tags: , , , | No Comments »

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.

  1.  
  2. $custom_post_types = array();
  3.  
  4. add_action(‘init’, ‘init_custom_post_types’);
  5. if(!is_admin())
  6.   add_filter(‘pre_get_posts’, ’set_custom_post_types’);
  7.  
  8. function init_custom_post_types()
  9. {
  10.   global $wpdb, $custom_post_types;
  11.  
  12.   // Create a default custom ‘post_type’ called custom_post_types.
  13.   // This adds a "Post Types" UI panel in the admin and allows us
  14.   // to create new post types on the fly just by adding a new post
  15.   // through the admin interface.
  16.   $post_type_args = array(
  17.     ‘label’=>__(‘Post Types’),
  18.     ‘public’=>true,
  19.     ’show_ui’=>true,
  20.     ‘exclude_from_search’=>true
  21.   );
  22.   register_post_type(‘custom_post_types’, $post_type_args);
  23.  
  24.   // If there are any posts with the ‘post_type’ of custom_post_types
  25.   // their names and titles will be returned and new ‘post_types’ will
  26.   // automatically be registered for each record.
  27.   $sql = "
  28.    SELECT
  29.      {$wpdb->posts}.post_name,
  30.      {$wpdb->posts}.post_title
  31.    FROM
  32.      {$wpdb->posts}
  33.    WHERE
  34.      {$wpdb->posts}.post_type = ‘custom_post_types’
  35.      AND {$wpdb->posts}.post_status = ‘publish’";
  36.  
  37.   foreach($post_types = $wpdb->get_results($sql) as $post_type)
  38.   {
  39.     // Store a global array of custom post types for modifying the
  40.     // search query to include the new post types we just registered
  41.     $custom_post_types[$post_type->post_name] = array(
  42.       ‘name’=>$post_type->post_name,
  43.       ‘title’=>$post_type->post_title
  44.     );
  45.  
  46.     $post_type_args = array(
  47.       ‘taxonomies’=>array(‘post_tag’,‘category’),
  48.       ‘label’=>__((string)$post_type->post_title),
  49.       ‘public’=>true,
  50.       ’show_ui’=>true,
  51.       ‘exclude_from_search’=>false,
  52.       ‘register_meta_box_cb’=>‘init_custom_meta_boxes’,
  53.       ‘_builtin’=>true,
  54.       ’supports’=>array(‘title’,‘editor’,‘thumbnail’,‘excerpts’,‘trackbacks’,‘custom-fields’,‘comments’,‘author’,‘revisions’)
  55.     );
  56.     register_post_type((string)$post_type->post_name, $post_type_args);
  57.   }
  58. }
  59.  
  60. function set_custom_post_types($query)
  61. {
  62.   global $custom_post_types;
  63.  
  64.   if($custom_post_types)
  65.   {
  66.     // Here we create an array of all valid post types
  67.     // that we want to appear on the blog.
  68.     $post_types = array(‘post’, ‘attachment’); //’page’,
  69.     foreach($custom_post_types as $key=>$post_type)
  70.     {
  71.       $post_types[] = $post_type[‘name’];
  72.     }
  73.   }
  74.  
  75.   // Now we modify the query to include the newly registed post types
  76.     $query->set(‘post_type’, $post_types);
  77.     return $query;
  78. }
  79.  
  80. function init_custom_meta_boxes($args)
  81. {
  82.   add_meta_box(’setcustommeta’, __(‘Attributes’), ’set_custom_meta_boxes’, $args->post_type, ‘normal’, ‘core’);
  83. }
  84.  
  85. function set_custom_meta_boxes($post)
  86. {
  87.   // add custom meta boxes
  88. }
  89.  

Everything a Web Developer Needs to Get Started

Posted: April 10th, 2009 | Author: David Scott Tufts | Filed under: Getting Started, Resources, Web Development | Tags: , , , , | 2 Comments »

Ever consider Web Development as a career? The following list of free resources will help you get up-and-running as a Web Developer.

PHP – Currently the most popular and widely used web development platform in the world.  After starting my software/web development career several years ago in the world of Microsoft, it was a relief to see that there was a simple solution out there that made far more sense (and was free).  PHP is also the “P” in LAMP, which is an acronym for Linux, Apache, MySQL, and PHP – a combination of technologies forming an open source development environment.
XAMPP
– Configuring a web development environment couldn’t be easier, with XAMPP a development server can be up and running within a half hour instead of several hours.  It installs and configures PHP, MySQL, and Apache on your operating system.
PSPad
– There are many great text editors out there, I just happen to prefer PSPad.
FireFox
– A browser that can be extended by installing web developer friendly plugins, here are my favorites:

  • Firebug – Allows you to edit, debug, and monitor CSS, HTML, and JavaScript live in any web page
  • Web Developer Toolbar – Adds a menu and a toolbar with various web developer tools
  • MeasureIt – Draw out a ruler to get the pixel width and height of any elements on a webpage
  • ColorZilla – Advanced eyedropper, color picker, page zoomer and more
  • HTML Validator – Adds HTML validation inside Firefox
  • Operator – Finds microformats and other semantic data that are on many web pages
  • ScreenGrab – Captures an image of what you can see in the window, the entire page, just a selection
  • YSlow – Analyzes web pages and tells you why they’re slow based on Yahoo’s high performance rules

jQuery – A JavaScript library and AJAX utility.
Wordpress and WordPress MU – Blogging tools and CMS publishing platforms. (not just for blogs anymore)
TinyMCE – A JavaScript WYSIWYG editor for content entry on your websites.
phpMailer – An advanced PHP class for sending emails from your web applications.
SWFUploader – A multi-file flash uploading tool for your website.
Silk Icons – 1000 free web icons.
Gimp – A free image editing tool which is a great alternative to Adobe’s Photoshop.