pierrebalian
9/25/2017 - 5:42 PM

Create a custom post type w/ a custom taxonomy

Create a custom post type w/ a custom taxonomy

function register_custom_post_types() {	

	/**
	 * Array of Service Labels for register_post_type
	 *
	 * @var array $service_labels
	 */
	$service_labels = array(
		'name'                  => _x( 'Services', 'Post Type General Name', 'textdomain-cpts' ),
		'singular_name'         => _x( 'Service', 'Post Type Singular Name', 'textdomain-cpts' ),
		'menu_name'             => __( 'Services', 'textdomain-cpts' ),
		'name_admin_bar'        => __( 'Service', 'textdomain-cpts' ),
		'archives'              => __( 'Service Archives', 'textdomain-cpts' ),
		'parent_item_colon'     => __( 'Parent Service:', 'textdomain-cpts' ),
		'all_items'             => __( 'All Services', 'textdomain-cpts' ),
		'add_new_item'          => __( 'Add New Service', 'textdomain-cpts' ),
		'add_new'               => __( 'Add New', 'textdomain-cpts' ),
		'new_item'              => __( 'New Service', 'textdomain-cpts' ),
		'edit_item'             => __( 'Edit Service', 'textdomain-cpts' ),
		'update_item'           => __( 'Update Service', 'textdomain-cpts' ),
		'view_item'             => __( 'View Service', 'textdomain-cpts' ),
		'search_items'          => __( 'Search Service', 'textdomain-cpts' ),
		'not_found'             => __( 'Not found', 'textdomain-cpts' ),
		'not_found_in_trash'    => __( 'Not found in Trash', 'textdomain-cpts' ),
		'featured_image'        => __( 'Featured Image', 'textdomain-cpts' ),
		'set_featured_image'    => __( 'Set featured image', 'textdomain-cpts' ),
		'remove_featured_image' => __( 'Remove featured image', 'textdomain-cpts' ),
		'use_featured_image'    => __( 'Use as featured image', 'textdomain-cpts' ),
		'insert_into_item'      => __( 'Insert into service', 'textdomain-cpts' ),
		'uploaded_to_this_item' => __( 'Uploaded to this item', 'textdomain-cpts' ),
		'items_list'            => __( 'Services list', 'textdomain-cpts' ),
		'items_list_navigation' => __( 'Services list navigation', 'textdomain-cpts' ),
		'filter_items_list'     => __( 'Filter Services list', 'textdomain-cpts' ),
	);

	/**
	 * An array of service arguments for register_post_type
	 *
	 * @var array $service_args
	 */
	$service_args = array(
		'label'               => __( 'Service', 'textdomain-cpts' ),
		'labels'              => $service_labels,
		'supports'            => array(
			'title',
			'editor',
			'thumbnail',
			'excerpt',
			'custom-fields',
			'page-attributes',
			'revisions',
		),
		'hierarchical'        => true,
		'public'              => true,
		'show_ui'             => true,
		'show_in_menu'        => true,
		'menu_position'       => 30,
		'menu_icon'           => 'dashicons-format-chat',
		'show_in_admin_bar'   => true,
		'show_in_nav_menus'   => true,
		'can_export'          => true,
		'taxonomies'          => array(
			'product_line',
			'product_color',
		),
		'has_archive'         => false,
		'exclude_from_search' => false,
		'publicly_queryable'  => true,
		'capability_type'     => 'page',
	);

	register_post_type( 'service', $service_args );
	
	/**
	 * Taxonomy for Service CPT
	 *
	 * @var array $service_type_labels
	 */
	$service_type_labels = array(
		'name'                       => _x( 'Service Types', 'Taxonomy General Name', 'textdomain-cpts' ),
		'singular_name'              => _x( 'Service Type', 'Taxonomy Singular Name', 'textdomain-cpts' ),
		'menu_name'                  => __( 'Service Type', 'textdomain-cpts' ),
		'all_items'                  => __( 'All Service Types', 'textdomain-cpts' ),
		'parent_item'                => __( 'Parent Service Type', 'textdomain-cpts' ),
		'parent_item_colon'          => __( 'Parent Service Type:', 'textdomain-cpts' ),
		'new_item_name'              => __( 'New Service Type Name', 'textdomain-cpts' ),
		'add_new_item'               => __( 'Add New Service Type', 'textdomain-cpts' ),
		'edit_item'                  => __( 'Edit Service Type', 'textdomain-cpts' ),
		'update_item'                => __( 'Update Service Type', 'textdomain-cpts' ),
		'view_item'                  => __( 'View Service Type', 'textdomain-cpts' ),
		'separate_items_with_commas' => __( 'Separate Service Types with commas', 'textdomain-cpts' ),
		'add_or_remove_items'        => __( 'Add or remove Service Types', 'textdomain-cpts' ),
		'choose_from_most_used'      => __( 'Choose from the most used', 'textdomain-cpts' ),
		'popular_items'              => __( 'Popular Service Types', 'textdomain-cpts' ),
		'search_items'               => __( 'Search Service Types', 'textdomain-cpts' ),
		'not_found'                  => __( 'Not Found', 'textdomain-cpts' ),
		'no_terms'                   => __( 'No Service Types', 'textdomain-cpts' ),
		'items_list'                 => __( 'Service Types list', 'textdomain-cpts' ),
		'items_list_navigation'      => __( 'Service Types list navigation', 'textdomain-cpts' ),
	);

	$service_type_args   = array(
		'labels'            => $service_type_labels,
		'hierarchical'      => true,
		'public'            => true,
		'show_ui'           => true,
		'show_admin_column' => true,
		'description'       => __( 'The type of service.', 'textdomain-cpts' ),
		'show_in_nav_menus' => true,
		'show_tagcloud'     => false,
		'rewrite'           => array(
			'slug' => 'service_type',
		),
	);

	register_taxonomy( 'service_type', array( 'service' ), $service_type_args );

	register_taxonomy_for_object_type( 'service_type', 'service' ); 
	
}

add_action( 'init', register_custom_post_types, 0 );