Custom Post Types and Taxonomies
Monday, October 13th, 2014
This is an extension of my 2014 High Ed Web presentation: WordPress and Beer
Custom post types
In WordPress everything is a post (well… almost everything). Custom post types are a convenient way to store all of your data needed for the application.
Here we have created post types for: Grains, hops, yeasts, water, styles and recipes.
To do this, we simply attach ‘register_post_type’ to the initialization hook. ‘init’ works well because it runs after WordPress has finished loading but before any headers are sent.
[php]
// Grain
add_action(‘init’, ‘agb_register_post_grain’);
function agb_register_post_grain() {
   register_post_type(‘agb_grain’,
       array(
           ‘label’ => ‘Grain’,
           ‘description’ => ‘Ingredient: grain’,
           ‘public’ => true,
           ‘show_ui’ => true,
           ‘show_in_menu’ => true,
           ‘capability_type’ => ‘post’,
           ‘map_meta_cap’ => true,
           ‘hierarchical’ => false,
           ‘rewrite’ => array(‘slug’ => ‘grain’, ‘with_front’ => 1),
           ‘query_var’ => true,
           ‘menu_position’ => ’50’,
           ‘menu_icon’ => ‘dashicons-carrot’,
           ‘supports’ => array(‘title’,’editor’,’excerpt’,’trackbacks’,’custom-fields’,’comments’,’revisions’,’thumbnail’),
           ‘labels’ => array (
             ‘name’ => ‘Grain’,
             ‘singular_name’ => ‘grain’,
             ‘menu_name’ => ‘Grain’,
             ‘add_new’ => ‘Add grain’,
             ‘add_new_item’ => ‘Add New grain’,
             ‘edit’ => ‘Edit’,
             ‘edit_item’ => ‘Edit grain’,
             ‘new_item’ => ‘New grain’,
             ‘view’ => ‘View grain’,
             ‘view_item’ => ‘View grain’,
             ‘search_items’ => ‘Search Grain’,
             ‘not_found’ => ‘No Grain Found’,
             ‘not_found_in_trash’ => ‘No Grain Found in Trash’,
             ‘parent’ => ‘Parent grain’,
           )
       )
   );
}
[/php]
There’s a lot included in that code, but most of if is used to set various labels names. Here are a few items to really pay attention to:
[php]register_post_type(‘agb_grain’…[/php]
This specifies your post type. Use something unique, but meaningful. You will be querying for by this name from now on.
[php]’supports’ => array(‘title’,’editor’,’excerpt’,’trackbacks’…[/php]
What do we want to be available on the admin page? Sometime you need it all. Other times you may only need one, or two items.
[php]’menu_icon’ => ‘dashicons-carrot’…[/php]
Sets the admin icon for the post type. Default is the pin use for posts. This can be the full URL path to an image, or the name of one of the included dashicons. In this case, I picked a carrot… because carrot.
Custom taxonomies
You can also easily create custom “tags” and “categories”. The only difference between the two is that categories are “hierarchical => true”.
[php]
// Yeast source
add_action(‘init’, ‘agb_register_tax_yeast_source’);
   function agb_register_tax_yeast_source() {
       register_taxonomy( ‘agb_yeast_source’,array (‘agb_yeast’),
       array(
           ‘hierarchical’ => true,
           ‘label’ => ‘Yeast Source’,
           ‘show_ui’ => true,
           ‘query_var’ => true,
           ‘rewrite’ => array( ‘slug’ => ‘yeast-source’ ),
           ‘show_admin_column’ => false
       )
   );
}
[/php]
Everything?
Here is all the code together:
gabriel nagmay (dot com) | Archive » Custom Fields and Meta Boxes (WordPress & Beer) Says:
[…] our example, each of the brewing ingredients is stored a custom post type. There is also a bunch of data that needs to be associated with each ingredient – this is […]
gabriel nagmay (dot com) | Archive » Caching Application Data (WordPress & Beer) Says:
[…] Start by hooking your function to the save of your custom post type: […]
gabriel nagmay (dot com) | Archive » WordPress and Beer: Homebrew web applications with WP Says:
[…] Custom Post Types and Taxonomies […]