Screen Shot 2013-07-09 at 8.32.27 PMI’m working on a random quote generator for one of my sites. I decided to create a custom post type called ‘quote’. I wanted to create a custom taxonomy for quote categories and another for quote tags since I want these categories and tags to be separate from the other categories and tags on the site.


First, I needed to register the quote custom post type:

register_post_type( 'quote',
		array(
			'labels' => array(
				'name' => __( 'Quotes' ),
				'singular_name' => __( 'Quote' ),
				'add_new' => __( 'Add New Quote' ),
				'add_new_item' => __( 'Add Quote' ),
				'edit' => __( 'Edit' ),
				'edit_item' => __( 'Edit Quote' ),
				'new_item' => __( 'New Quote' ),
				'view' => __( 'View Quote' ),
				'view_item' => __( 'View Quote' ),
				'search_items' => __( 'Search Items' ),
				'not_found' => __( 'No Quotes Found' ),
				'not_found_in_trash' => __( 'No Quotes Found in Trash' ),
				'parent' => __( 'Parent Event' ),
			),
			'public' => true,
			'show_ui' => true,
			'publicly_queryable' => true,
			'exclude_from_search' => false,
			'menu_position' => 20,
			'hierarchical' => true,
			'query_var' => true,
			'supports' => array( 'title', 'editor', 'thumbnail' ),
			'has_archive' => true,
			'rewrite' => array('slug' => 'quotes'),
		)
	);

Next we have to set up the variables and register the quote category custom taxonomy:

$labels = array(
		'name' => __( 'Quote Categories' ),
		'singular_name' => __( 'Quote Category' ),
		'add_new' => __( 'Add New Quote Category'),
		'add_new_item' => __( 'Add Quote Category' ),
		'edit_item' => __( 'Edit Quote Category' ),
		'new_item' => __( 'New Quote Category' ),
		'view_item' => __( 'View Quote Category' ),
		'search_items' => __( 'Search Quote Category' ),
		'not_found' => __( 'No Quote Category found' ),
		'not_found_in_trash' => __( 'No Quote Category found in Trash' ),
	);

$pages = array( 'quote' );

$args = array(
		'labels' => $labels,
		'singular_label' => __( 'Quote Category' ),
		'public' => true,
		'show_ui' => true,
		'hierarchical' => true,
		'show_tagcloud' => false,
		'show_in_nav_menus' => true,
		'rewrite' => array('slug' => 'quote-categories'),
	 );

register_taxonomy( 'quote_categories' , $pages , $args );

And then the custom quote tags taxonomy:

$labels2 = array(
		'name' => __( 'Quote Tags' ),
		'singular_name' => __( 'Quote Tag' ),
		'add_new' => __( 'Add New Quote Tag'),
		'add_new_item' => __( 'Add Quote Tag' ),
		'edit_item' => __( 'Edit Quote Tag' ),
		'new_item' => __( 'New Quote Tag' ),
		'view_item' => __( 'View Quote Tag' ),
		'search_items' => __( 'Search Quote Tag' ),
		'not_found' => __( 'No Quote Tag found' ),
		'not_found_in_trash' => __( 'No Quote Tag found in Trash' ),
	);

$pages2 = array( 'quote' );

$args2 = array(
		'labels' => $labels2,
		'singular_label' => __( 'Quote Tags' ),
		'public' => true,
		'show_ui' => true,
		'hierarchical' => false,
		'show_tagcloud' => true,
		'show_in_nav_menus' => true,
		'rewrite' => array('slug' => 'quote-tags'),
	 );

register_taxonomy( 'quote_tags' , $pages , $args2 );

One more thing we need to do is to refresh the rewrite rules so that our new permalinks will work for our new post type and taxonomies:

flush_rewrite_rules( false );

We now have a new custom post type for quotes along with quote categories and tags that show up in our WordPress Admin menu. Each quote will be added as a separate ‘post’.

add_action( 'init', 'create_quotes_post_type' );
function create_quotes_post_type() {
	register_post_type( 'quote',
		array(
			'labels' => array(
				'name' => __( 'Quotes' ),
				'singular_name' => __( 'Quote' ),
				'add_new' => __( 'Add New Quote' ),
				'add_new_item' => __( 'Add Quote' ),
				'edit' => __( 'Edit' ),
				'edit_item' => __( 'Edit Quote' ),
				'new_item' => __( 'New Quote' ),
				'view' => __( 'View Quote' ),
				'view_item' => __( 'View Quote' ),
				'search_items' => __( 'Search Items' ),
				'not_found' => __( 'No Quotes Found' ),
				'not_found_in_trash' => __( 'No Quotes Found in Trash' ),
				'parent' => __( 'Parent Event' ),
			),
			'public' => true,
			'show_ui' => true,
			'publicly_queryable' => true,
			'exclude_from_search' => false,
			'menu_position' => 20,
			'hierarchical' => true,
			'query_var' => true,
			'supports' => array( 'title', 'editor', 'thumbnail' ),
			'has_archive' => true,
			'rewrite' => array('slug' => 'quotes'),
		)
	);

	$labels = array(
		'name' => __( 'Quote Categories' ),
		'singular_name' => __( 'Quote Category' ),
		'add_new' => __( 'Add New Quote Category'),
		'add_new_item' => __( 'Add Quote Category' ),
		'edit_item' => __( 'Edit Quote Category' ),
		'new_item' => __( 'New Quote Category' ),
		'view_item' => __( 'View Quote Category' ),
		'search_items' => __( 'Search Quote Category' ),
		'not_found' => __( 'No Quote Category found' ),
		'not_found_in_trash' => __( 'No Quote Category found in Trash' ),
	);

	$labels2 = array(
		'name' => __( 'Quote Tags' ),
		'singular_name' => __( 'Quote Tag' ),
		'add_new' => __( 'Add New Quote Tag'),
		'add_new_item' => __( 'Add Quote Tag' ),
		'edit_item' => __( 'Edit Quote Tag' ),
		'new_item' => __( 'New Quote Tag' ),
		'view_item' => __( 'View Quote Tag' ),
		'search_items' => __( 'Search Quote Tag' ),
		'not_found' => __( 'No Quote Tag found' ),
		'not_found_in_trash' => __( 'No Quote Tag found in Trash' ),
	);

	$pages = array( 'quote' );
	$pages2 = array( 'quote' );

	$args = array(
		'labels' => $labels,
		'singular_label' => __( 'Quote Category' ),
		'public' => true,
		'show_ui' => true,
		'hierarchical' => true,
		'show_tagcloud' => false,
		'show_in_nav_menus' => true,
		'rewrite' => array('slug' => 'quote-categories'),
	 );
	$args2 = array(
		'labels' => $labels2,
		'singular_label' => __( 'Quote Tags' ),
		'public' => true,
		'show_ui' => true,
		'hierarchical' => false,
		'show_tagcloud' => true,
		'show_in_nav_menus' => true,
		'rewrite' => array('slug' => 'quote-tags'),
	 );
	register_taxonomy( 'quote_categories' , $pages , $args );
	register_taxonomy( 'quote_tags' , $pages , $args2 );

	flush_rewrite_rules( false );
}

4 thoughts on “Adding a WordPress Custom Post Type with Custom Taxonomies”

  1. Pingback: Cullen Web Services | Add a WordPress Meta Box to a Post

  2. Pingback: How to Choose a Random Post with a Custom Post Type in a Wordpress Shortcode - Cullen Web Services

  3. Pingback: Post to a Facebook Wall from my WordPress Website - Cullen Web Services

  4. Pingback: Display My Custom Post Categories (Custom Taxonomy) in a Dropdown - Cullen Web Services | Cullen Web Services

Leave a Comment

Your email address will not be published. Required fields are marked *