Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

Preview:

DESCRIPTION

Tracey Kemp takes us through the use of Custom Post Types and Taxonomies in Wordpress 3+ #wcsyd

Citation preview

DEMYSTIFYING CUSTOM POST TYPES & TAXONOMIESTracey Kemp@dogshindleg

➡ Registering Custom Post Types and Taxonomies

➡ A plugin that can help

➡ Displaying content from custom post types and taxonomies

➡ Resources

A record label site with artists and their releases.

FINAL RESULT

Custom Post Type Taxonomy

Artist Location

The Drones VIC

The Kill Devil Hills WA

Midget NSW / QLD

Front End Loader NSW

Dan Kelly VIC

Custom Post Type Taxonomy Taxonomy

Release Artist Year

Here Come The Lies The Drones 2002

Wait Long By The River The Drones 2005

Gala Mill The Drones 2006

Havilah The Drones 2008

Heathen Songs The Kill Devil Hills 2004

The Drought The Kill Devil Hills 2006

Man, You Should Explode The Kill Devil Hills 2010

Vagus Wandering Midget 1994

The Toggle Switch Midget 1997

Total Abandonment Of Better Understanding

Midget 1998

Let's Ride Front End Loader 1995

What will be a custom post type and what will be a taxonomy?

PLANNING

REGISTER CUSTOM POST TYPES

function register_custom_post_types() {

}

add_action(‘init’, ‘register_custom_post_types’);

// register post types hereREGISTER CUSTOM POST TYPES

REGISTER ARTISTS

function register_custom_post_types() {

}

add_action(‘init’, ‘register_custom_post_types’);

register_post_type( ‘artists’, array( ‘labels’ => array( ‘name’ => _x(‘Artists’, ‘post type general name’), ‘singular_name’ => __( ‘Artist’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Artists’ ), ‘edit_item’ => __( ‘Edit Artist’ ), ‘add_new_item’ => __( ‘Add New Artist’ ), ‘new_item_name’ => __( ‘New Artist’ ), ), ‘public’ => true, ‘show_ui’ => true, ‘hierarchical’ => true, ‘supports’ => array (‘title’,‘editor’, ‘thumbnail’) ) );

REGISTER ARTISTS

REGISTER RELEASES

function register_custom_post_types() {

}

add_action(‘init’, ‘register_custom_post_types’);

register_post_type( ‘artists’, array( ‘labels’ => array( ‘name’ => _x(‘Artists’, ‘post type general name’), ‘singular_name’ => __( ‘Artist’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Artists’ ), ‘edit_item’ => __( ‘Edit Artist’ ), ‘add_new_item’ => __( ‘Add New Artist’ ), ‘new_item_name’ => __( ‘New Artist’ ), ), ‘public’ => true, ‘show_ui’ => true, ‘hierarchical’ => true, ‘supports’ => array (‘title’,‘editor’, ‘thumbnail’) ) );

register_post_type( ‘releases’, array( ‘labels’ => array( ‘name’ => _x(‘Releases’, ‘post type general name’), ‘singular_name’ => __( ‘Release’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Releases’ ), ‘edit_item’ => __( ‘Edit Release’ ), ‘add_new_item’ => __( ‘Add New Release’ ), ‘new_item_name’ => __( ‘New Release’ ), ), ‘public’ => true, ‘show_ui’ => true, ‘hierarchical’ => true, ‘supports’ => array (‘title’, ‘editor’, ‘thumbnail’) ) );

REGISTER ARTISTS

REGISTER RELEASES

function register_custom_post_types() {

}

add_action(‘init’, ‘register_custom_post_types’);

register_post_type( ‘artists’, array( ‘labels’ => array( ‘name’ => _x(‘Artists’, ‘post type general name’), ‘singular_name’ => __( ‘Artist’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Artists’ ), ‘edit_item’ => __( ‘Edit Artist’ ), ‘add_new_item’ => __( ‘Add New Artist’ ), ‘new_item_name’ => __( ‘New Artist’ ), ), ‘public’ => true, ‘show_ui’ => true, ‘hierarchical’ => true, ‘supports’ => array (‘title’,‘editor’, ‘thumbnail’) ) );

register_post_type( ‘releases’, array( ‘labels’ => array( ‘name’ => _x(‘Releases’, ‘post type general name’), ‘singular_name’ => __( ‘Release’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Releases’ ), ‘edit_item’ => __( ‘Edit Release’ ), ‘add_new_item’ => __( ‘Add New Release’ ), ‘new_item_name’ => __( ‘New Release’ ), ), ‘public’ => true, ‘show_ui’ => true, ‘hierarchical’ => true, ‘supports’ => array (‘title’, ‘editor’, ‘thumbnail’) ) );

REGISTER ARTISTS

REGISTER RELEASES

function register_custom_post_types() {

}

add_action(‘init’, ‘register_custom_post_types’);

register_post_type( ‘artists’, array( ‘labels’ => array( ‘name’ => _x(‘Artists’, ‘post type general name’), ‘singular_name’ => __( ‘Artist’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Artists’ ), ‘edit_item’ => __( ‘Edit Artist’ ), ‘add_new_item’ => __( ‘Add New Artist’ ), ‘new_item_name’ => __( ‘New Artist’ ), ), ‘public’ => true, ‘show_ui’ => true, ‘hierarchical’ => true, ‘supports’ => array (‘title’,‘editor’, ‘thumbnail’) ) );

REGISTER TAXONOMIES

// register post types hereREGISTER TAXONOMIES

function register_taxonomies() {

}

add_action(‘init’, ‘register_taxonomies’, 0);

$labels = array( ‘name’ => _x( ‘Location’, ‘taxonomy general name’ ), ‘singular_name’ => _x( ‘Location’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Locations’ ), ‘edit_item’ => __( ‘Edit Location’ ), ‘add_new_item’ => __( ‘Add New Location’ ), ‘menu_name’ => __( ‘Locations’ ), ); register_taxonomy( ‘locations’, array(‘artists’), // post-type(s) array( ‘hierarchical’ => true, ‘labels’ => $labels, ‘show_ui’ => true, ) );

REGISTER LOCATION

}

add_action(‘init’, ‘register_taxonomies’, 0);

function register_taxonomies() {

REGISTER LOCATION

REGISTER RELEASE YEAR

function register_taxonomies() {

$labels = array( ‘name’ => _x( ‘Location’, ‘taxonomy general name’ ), ‘singular_name’ => _x( ‘Location’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Locations’ ), ‘edit_item’ => __( ‘Edit Location’ ), ‘add_new_item’ => __( ‘Add New Location’ ), ‘menu_name’ => __( ‘Locations’ ), ); register_taxonomy( ‘locations’, array(‘artists’), // post-type(s) array( ‘hierarchical’ => true, ‘labels’ => $labels, ‘show_ui’ => true, ) );

}

add_action(‘init’, ‘register_taxonomies’, 0);

$labels = array( ‘name’ => _x( ‘Release Year’, ‘taxonomy general name’ ), ‘singular_name’ => _x( ‘Release Year’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Release Years’ ), ‘edit_item’ => __( ‘Edit Release Year’ ), ‘add_new_item’ => __( ‘Add New Release Year’ ), ‘menu_name’ => __( ‘Release Years’ ), ); register_taxonomy( ‘release-year’, array(‘releases’), // post-type(s) array( ‘hierarchical’ => true, ‘labels’ => $labels, ‘show_ui’ => true, ) );

REGISTER LOCATION

REGISTER RELEASE YEAR

function register_taxonomies() {

$labels = array( ‘name’ => _x( ‘Location’, ‘taxonomy general name’ ), ‘singular_name’ => _x( ‘Location’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Locations’ ), ‘edit_item’ => __( ‘Edit Location’ ), ‘add_new_item’ => __( ‘Add New Location’ ), ‘menu_name’ => __( ‘Locations’ ), ); register_taxonomy( ‘locations’, array(‘artists’), // post-type(s) array( ‘hierarchical’ => true, ‘labels’ => $labels, ‘show_ui’ => true, ) );

}

add_action(‘init’, ‘register_taxonomies’, 0);

$labels = array( ‘name’ => _x( ‘Release Year’, ‘taxonomy general name’ ), ‘singular_name’ => _x( ‘Release Year’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Release Years’ ), ‘edit_item’ => __( ‘Edit Release Year’ ), ‘add_new_item’ => __( ‘Add New Release Year’ ), ‘menu_name’ => __( ‘Release Years’ ), ); register_taxonomy( ‘release-year’, array(‘releases’), // post-type(s) array( ‘hierarchical’ => true, ‘labels’ => $labels, ‘show_ui’ => true, ) );

REGISTER LOCATION

REGISTER RELEASE YEAR

function register_taxonomies() {

$labels = array( ‘name’ => _x( ‘Location’, ‘taxonomy general name’ ), ‘singular_name’ => _x( ‘Location’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Locations’ ), ‘edit_item’ => __( ‘Edit Location’ ), ‘add_new_item’ => __( ‘Add New Location’ ), ‘menu_name’ => __( ‘Locations’ ), ); register_taxonomy( ‘locations’, array(‘artists’), // post-type(s) array( ‘hierarchical’ => true, ‘labels’ => $labels, ‘show_ui’ => true, ) );

}

add_action(‘init’, ‘register_taxonomies’, 0);

USING A CUSTOM POST TYPE AS A TAXONOMY

DISPLAY ARTISTS

<?php //artists posts $artist_args=array( 'post_type' => 'artists', 'posts_per_page' => -1, ); $artist_query = null; $artist_query = new WP_Query($artist_args); if( $artist_query->have_posts() ) { while ($artist_query->have_posts()) : $artist_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <h2><?php the_title(); ?></h2> <?php the_excerpt(); ?> </article> <?php endwhile; } wp_reset_query(); ?>

DISPLAY ARTISTS

<?php //artists posts $artist_args=array( 'post_type' => 'artists', 'posts_per_page' => -1, ); $artist_query = null; $artist_query = new WP_Query($artist_args); if( $artist_query->have_posts() ) { while ($artist_query->have_posts()) : $artist_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <h2><?php the_title(); ?></h2>

<?php the_excerpt(); ?> </article> <?php endwhile; } wp_reset_query(); ?>

<?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?>

DISPLAY ARTISTS WITH LOCATIONS

DISPLAY ARTISTS WITH LOCATIONS

<?php //artists posts $artist_args=array( 'post_type' => 'artists', 'posts_per_page' => -1, ); $artist_query = null; $artist_query = new WP_Query($artist_args); if( $artist_query->have_posts() ) { while ($artist_query->have_posts()) : $artist_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <h2><?php the_title(); ?></h2>

<?php the_excerpt(); ?> </article> <?php endwhile; } wp_reset_query(); ?>

<?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?>

<?php echo get_the_term_list( $id, $taxonomy, $before, $sep, $after ) ?>

<?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?>

<?php //releases posts $release_args=array( 'post_type' => 'releases', 'posts_per_page' => -1, ); $release_query = null; $release_query = new WP_Query($release_args); if( $release_query->have_posts() ) { while ($release_query->have_posts()) : $release_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <?php the_title(); ?> <?php echo get_the_term_list( $post->ID, ‘release-year’, ‘(’, ‘‘, ‘)’ ); ?> </article> <?php endwhile; } wp_reset_query(); ?>

DISPLAY RELEASES

<?php //releases posts $release_args=array( 'post_type' => 'releases', 'posts_per_page' => -1, ); $release_query = null; $release_query = new WP_Query($release_args); if( $release_query->have_posts() ) { while ($release_query->have_posts()) : $release_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <?php the_title(); ?> <?php echo get_the_term_list( $post->ID, ‘release-year’, ‘(’, ‘‘, ‘)’ ); ?> </article> <?php endwhile; } wp_reset_query(); ?>

DISPLAY RELEASES

HOW DO WE SHOW ARTISTS WITH RELEASES?

<?php //artists posts $artist_args=array( 'post_type' => 'artists', 'posts_per_page' => -1, ); $artist_query = null; $artist_query = new WP_Query($artist_args); if( $artist_query->have_posts() ) { while ($artist_query->have_posts()) : $artist_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <h2><?php the_title(); ?></h2> <?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?> <?php the_excerpt(); ?>

//insert releases here

</article> <?php endwhile; } wp_reset_query(); ?>

DISPLAY ARTISTS

if( $artist_query->have_posts() ) { while ($artist_query->have_posts()) : $artist_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <h2><?php the_title(); ?></h2> <?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?> <?php the_excerpt(); ?>

</article>

<?php //releases posts $release_args=array( 'post_type' => 'releases', 'posts_per_page' => -1, ); $release_query = null; $release_query = new WP_Query($release_args); if( $release_query->have_posts() ) { while ($release_query->have_posts()) : $release_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <?php the_title(); ?> <?php echo get_the_term_list( $post->ID, ‘release-year’, ‘(’, ‘‘, ‘)’ ); ?> </article> <?php endwhile; } wp_reset_query(); ?>

DISPLAY ARTISTS

DISPLAY RELEASES

if( $artist_query->have_posts() ) { while ($artist_query->have_posts()) : $artist_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <h2><?php the_title(); ?></h2> <?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?> <?php the_excerpt(); ?>

<?php //releases posts $release_args=array( 'post_type' => 'releases', 'posts_per_page' => -1, ); $release_query = null; $release_query = new WP_Query($release_args); if( $release_query->have_posts() ) { while ($release_query->have_posts()) : $release_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <?php the_title(); ?> <?php echo get_the_term_list( $post->ID, ‘release-year’, ‘(’, ‘‘, ‘)’ ); ?> </article> <?php endwhile; } wp_reset_query(); ?>

DISPLAY ARTISTS

DISPLAY RELEASES

GET ARTIST TITLE

if( $artist_query->have_posts() ) { while ($artist_query->have_posts()) : $artist_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <h2><?php the_title(); ?></h2> <?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?> <?php the_excerpt(); ?>

<?php $tax_artist = get_the_title(); ?>

<?php //releases posts $release_args=array( 'post_type' => 'releases', 'posts_per_page' => -1, ); $release_query = null; $release_query = new WP_Query($release_args); if( $release_query->have_posts() ) { while ($release_query->have_posts()) : $release_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <?php the_title(); ?> <?php echo get_the_term_list( $post->ID, ‘release-year’, ‘(’, ‘‘, ‘)’ ); ?> </article> <?php endwhile; } wp_reset_query(); ?>

DISPLAY ARTISTS

DISPLAY RELEASES

GET ARTIST TITLE

if( $artist_query->have_posts() ) { while ($artist_query->have_posts()) : $artist_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <h2><?php the_title(); ?></h2> <?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?> <?php the_excerpt(); ?>

<?php $tax_artist = get_the_title(); ?>

'artists' => $tax_artist,

<?php //releases posts $release_args=array( 'post_type' => 'releases',

'posts_per_page' => -1, ); $release_query = null; $release_query = new WP_Query($release_args); if( $release_query->have_posts() ) { while ($release_query->have_posts()) : $release_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <?php the_title(); ?> <?php echo get_the_term_list( $post->ID, ‘release-year’, ‘(’, ‘‘, ‘)’ ); ?> </article>

DISPLAY ARTISTS

DISPLAY RELEASES

$taxonomy => $term

GET ARTIST TITLE

<?php //artists posts $artist_args=array( 'post_type' => 'artists', 'posts_per_page' => -1, ); $artist_query = null; $artist_query = new WP_Query($artist_args); if( $artist_query->have_posts() ) { while ($artist_query->have_posts()) : $artist_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <h2><?php the_title(); ?></h2> <?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?> <?php the_excerpt(); ?>

<?php $tax_artist = get_the_title(); ?>

'artists' => $tax_artist,

<?php //releases posts $release_args=array( 'post_type' => 'releases',

'posts_per_page' => -1, ); $release_query = null; $release_query = new WP_Query($release_args); if( $release_query->have_posts() ) { while ($release_query->have_posts()) : $release_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <?php the_title(); ?> <?php echo get_the_term_list( $post->ID, ‘release-year’, ‘(’, ‘‘, ‘)’ ); ?> </article> <?php endwhile; } wp_reset_query(); ?>

</article> <?php endwhile; } wp_reset_query(); ?>

START ARTISTS QUERY

GET ARTIST TITLE

$taxonomy => $term

CLOSE ARTISTS

START RELEASES QUERY

CLOSE RELEASES

RESOURCES

http://codex.wordpress.org/http://wordpress.org/support/

WordPress Codex & Forums

Register Custom Post Typeshttp://codex.wordpress.org/Function_Reference/register_post_type

Register Taxonomieshttp://codex.wordpress.org/Function_Reference/register_taxonomy

Query Postshttp://codex.wordpress.org/Function_Reference/query_posts

The Loophttp://codex.wordpress.org/The_Loop

DEMYSTIFYING CUSTOM POST TYPES & TAXONOMIESTracey Kemp@dogshindleg

Full code samples and explanations

dogshindleg.com