The WordPress custom post types and taxonomies are a very powerful feature introduced in WordPress 3.0 that allow you to organise your site content much more effectively and provide a more usable interface in the back end of the site.

We been utilising these features in all our recent website builds and will be using the recently completed AMV Live Music website as an example.

AMV is a booking agent providing live musical entertainment for all occasions, focusing on the north east of England and Newcastle. We felt our website design worked well for the purpose, we just needed a good way of displaying the bands and making them easy to manipulate through the content management system.

We used custom post types to categorise the artists featured on the site as well as customise the view in the dashboard to make adding and managing artists that much easier.

Screenshot of the AMV Live Music Website

Setting up the artists post type

The first step for the site was to define a custom post type for adding artists to the site, this is added in our functions.php.

// Register Artists Post Type

function post_type_artists() {
register_post_type('artists', array(
'labels' => array(
    'name' => _x('Artists', 'post type general name'),
    'singular_name' => _x('Artist', 'post type singular name'),
    'add_new' => _x('Add New', 'artists'),
    'add_new_item' => __('Add New Artist'),
    'edit_item' => __('Edit Artist'),
    'new_item' => __('New Artist'),
    'view_item' => __('View Artist'),
    'search_items' => __('Search Artists'),
    'not_found' =>  __('No artists found'),
    'not_found_in_trash' => __('No artists found in Trash'),
    'parent_item_colon' => ''),
'public' => true,
'exclude_from_search'=> false,
'show_ui' => true,
'_builtin' => false,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array("slug" => "artists"),
'menu_icon' => '/wp-content/themes/amv/images/admin-icon-artist.png',
'supports' => array('title','editor','thumbnail')
));
}
add_action('init', 'post_type_artists');

Breaking this down, the first line gives our function a name “post_type_artists”. Then we use “register_post_type”  to define a post type called “artists” with the following options:

The artists admin panel as seen in the WordPress dashboardlabels – Here we can define the text used for various names and links related to this post type, the most important is “name” which is what your custom post type will be called.

public – This defines whether the post type is available through the dashboard.

exclude_from_search – Should posts of this type be included or excluded from the search results.

show_ui – Whether this post type uses the default user interface in the dashboard.

_builtin – This one catches a lot of people out and is the most comnmon cause of 404 errors when dealing with custom post types. This should be set to false as this isn’t a built in post type (Posts, pages and attachments) it’s a custom post type.

capability_type – This sets read, edit and delete capabilities. Most of the time you can leave it on post which replicates the same capabilities of standard posts.

hierarchical – Allows a parent to be defined

rewrite – Allows url rewriting of the post type so they have pretty links. Setting this to true will use the post types name as the slug, or you can define the slug as we have done.

menu_icon – By default the dashboard panels created for the post type will display a little paper and pencil icon, you can define your own here.

supports – Here you can specify what functionality the dashboard will allow for this post type: title, editor, author, thumbnail, excerpt, trackbacks, custom-fields, comments, revisions, page-attributes.

These are the most common parameters, more are available on the WordPress Codex, but you are unlikely to need them.

Register custom taxonomies for our post type.

Custom taxonomies allow us to add additional information to our custom posts, they come in two types: Hierarchical (These work like categories) and non-hierarchical (These work like tags). For our artists post type we will add a hierarchical taxonomy for “style” and a non-hierarchical one for “keywords”.

// Register Style Taxonomy

function create_style_taxonomies(){

register_taxonomy('style',array('artists'), array(
'hierarchical' => true,
    'labels' => array(
    'name' => _x( 'Styles', 'taxonomy general name' ),
    'singular_name' => _x( 'Style', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Styles' ),
    'popular_items' => __( 'Popular Styles' ),
    'all_items' => __( 'All Styles' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit Style' ),
    'update_item' => __( 'Update Style' ),
    'add_new_item' => __( 'Add New Style' ),
    'new_item_name' => __( 'New Style Name' ),
    'separate_items_with_commas' => __( 'Separate styles with commas' ),
    'add_or_remove_items' => __( 'Add or remove styles' ),
    'choose_from_most_used' => __( 'Choose from the most used styles' ),
    );
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'style' ),
));
}  

add_action( 'init', 'create_style_taxonomies', 0 );
// Register Keyword Taxonomy

function create_keyword_taxonomies() {

register_taxonomy('keywords',array('artists'), array(
'hierarchical' => false,
'labels' => array(
    'name' => _x( 'Keywords', 'taxonomy general name' ),
    'singular_name' => _x( 'Keyword', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Keywords' ),
    'popular_items' => __( 'Popular Keywords' ),
    'all_items' => __( 'All Keywords' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit Keyword' ),
    'update_item' => __( 'Update Keyword' ),
    'add_new_item' => __( 'Add New Keyword' ),
    'new_item_name' => __( 'New Keyword Name' ),
    'separate_items_with_commas' => __( 'Separate keywords with commas' ),
    'add_or_remove_items' => __( 'Add or remove keywords' ),
    'choose_from_most_used' => __( 'Choose from the most used keywords'),
    );
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'keyword' ),
));
}

add_action( 'init', 'create_keyword_taxonomies', 0 );

Again we have defined the labels and some other variables for our custom taxonomies. If you want more information on the various parameters have a look on the WordPress Codex Now when we add new artists we can select what style they are and add a list of keywords. Lets take this further though and customise our dashboard to show our new taxonomies when viewing our list of artists.

Customising the artists list view

WordPress allows you to define the columns shown on the manage posts page, you can select from the default columns as well as add your own custom ones. In our example when you view the list of artists we will want to see the style and keywords taxonomies we have added.

// Customise edit columns for Artists post type

add_filter("manage_edit-artists_columns", "prod_edit_columns");
add_action("manage_posts_custom_column",  "prod_custom_columns");

function prod_edit_columns($columns){
$columns = array(
"cb" => "<input type="checkbox" />",
"title" => "Artist Name",
"style" => "Style",
"keywords" => "Keywords",
"gallery" => "Gallery ID",
"date" => "Date",
);

return $columns;
}

function prod_custom_columns($column){
global $post;
switch ($column)
{
case "style":
echo get_the_term_list($post->ID, 'style', '', ', ','');
break;

case "gallery":
$custom = get_post_custom();
echo $custom["gallery_value"][0];
break;

case "keywords":
echo get_the_term_list($post->ID, 'keywords', '', ', ','');
break;
}
}

The first two lines simply set up our functions, the first function tells WordPress which columns we want to use, and the second defines the content of the custom columns. Where it says “manage_edit-artists_columns” you should replace the word “artists” with whatever the name is of your own custom post type.

In the “prod_edit_columns” function we simply list the columns we want. The default edit columns are all removed once you start defining your own so we need to add them back in. “title” and “date” are self explanitary, “cb” is the checkbox that allows you to select posts for deletion etc. “style”, “keywords” and “Gallery ID” are all custom columns that we will be defining in the next function.

“prod_custom_columns” is where we specify what data appears in our custom columns. “case” is simply the identifying name you give the column so you can refer to it in the first function, while the line “echo get_the_term_list($post->ID, ‘style’, ”, ‘, ‘,”);” pulls the data out of our custom taxonomy, in this case “style”.

While we haven’t mentioned it yet, “Gallery ID” is a column we will be using to pull in an ID number from a custom field called gallery_value. We are using this to allow a NextGEN Gallery to be associated with the artist.

Artists view in the WordPress dashboard