CUSTOM POST TYPES & TAXONOMIES Christopher Reding - @creding - April 18th 2011
What?Posts in Wordpress are stored in the wp_posts table. Within this table there is acolumn for the post type. Using this column Wordpress can determine the correctform of presentation or relationship the post requires when querying for that posttype.Custom Post Types are no different, they too are stored in the wp_posts table andhave all the characteristics of their sibling objects. The only real difference is youdefine them. The name, attributes, and abilities can be customized allowing you toexpand the possibilities of Wordpress beyond what many ever thought possiblewithout a plugin.
What?Taxonomies are the categories and tags we all know and love. They enable posts tobe divided and subdivided into relational containers making it a breeze to navigateas well as see related topics.Custom taxonomies are no different, they can do all the same things. The onlydifference is that you are in control of what that taxonomy is applied to and how itcan be used - just like custom post types.
Creating Your Custom Post TypeBeer $labels = array( name => _x(Beer, post type general name),Deﬁne Labels singular_name => _x(Beer, post type singular name), add_new => _x(Add New, beer), add_new_item => __(Add New Beer), edit_item => __(Edit Beer), new_item => __(New Beer), view_item => __(View Beer), search_items => __(Search For Beers), not_found => __(No beers found), not_found_in_trash => __(No beers found in Trash), parent_item_colon => , menu_name => Beers );
Creating Your Custom Post TypeBeer $args = array( labels => $labels, public => true, publicly_queryable => true, show_ui => true,Deﬁne Attributes show_in_menu => true, query_var => true, rewrite => true, capability_type => post, has_archive => true, hierarchical => false, menu_position => 10, supports => array(title,editor,author,thumbnail,excerpt,comments) );Register It register_post_type(beer,$args);
Custom Meta BoxesNow that you have a custom post type you may want to define some extra fields.Using a custom meta box you can create a form containing the fields you want touse. You then use that form to populate the post’s meta data. The wp_postmetatable in the database enables us to relate data to the post and give it a key name(meta_key => meta_value)This is made even easier using the WPAlchemy MetaBox PHP classhttp://www.farinspace.com/wpalchemy-metabox/
EVENT META BOXAdds a form to the event custom post type allowing specific event data to be stored.
AND THE FRONT END Event custom post type displayed from the front end.
Why?Pre-formatted content such as an event.To isolate content into silos making it easier on the content creators.Save the WP database from the ravage of excessive table bloat.A better integration for plugin creators who want to take advantage of built-in WPfunctions such as search, archives, and permalinks.