Recently, we learned how to create a simple custom post type. Allowing to register a post type is one of the great functionality of WordPress and what’s even greater is we can also assign a taxonomy into a custom post type.

Today, i will show you how to create a custom post type with a taxonomy assigned to it.

You can assign an existing taxonomy into a custom post type, a category taxonomy for example, or you can register a custom taxonomy while registering the custom post type.

In this tutorial, lets create a custom post type called 'portfolio' and assign a taxonomy called 'postfolio_type' into our custom post type. Let’s paste the code below into our theme’s functions.php.

add_action( 'init', 'register_my_post_type' );
function register_my_post_type() {
	//lets register our post type
	$labels = array(
        'name'               => _x( 'Portfolio', 'post type general name', 'text-domain' ),
        'singular_name'      => _x( 'Portfolio', 'post type singular name', 'text-domain' ),
        'menu_name'          => _x( 'Portfolios', 'admin menu', 'text-domain' ),
        'name_admin_bar'     => _x( 'Portfolio', 'add new on admin bar', 'text-domain' ),
        'add_new'            => _x( 'Add New', 'Portfolio', 'text-domain' ),
        'add_new_item'       => __( 'Add New Portfolio', 'text-domain' ),
        'new_item'           => __( 'New Portfolio', 'text-domain' ),
        'edit_item'          => __( 'Edit Portfolio', 'text-domain' ),
        'view_item'          => __( 'View Portfolio', 'text-domain' ),
        'all_items'          => __( 'All Portfolios', 'text-domain' ),
        'search_items'       => __( 'Search Portfolios', 'text-domain' ),
        'parent_item_colon'  => __( 'Parent Portfolios:', 'text-domain' ),
        'not_found'          => __( 'No Portfolio found.', 'text-domain' ),
        'not_found_in_trash' => __( 'No Portfolio found in Trash.', 'text-domain' )
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'portfolio' ),
        'menu_icon'          => 'dashicons-category',
        'supports'           => array( 'title', 'editor', 'thumbnail', 'revisions' ),
        'has_archive'        => true,
        'exclude_from_search'=> false
    );
    register_post_type( 'portfolio', $args );

	//lets register our taxonomy
	$labels = array(
			'name' => _x( 'Portfolio Type', 'taxonomy general name', 'text-domain' ),
			'singular_name' => _x( 'Portfolio Type', 'taxonomy singular name', 'text-domain' ),
			'search_items' =>  __( 'Search Portfolio Types', 'text-domain' ),
			'popular_items' => __( 'Popular Portfolio Types', 'text-domain' ),
			'all_items' => __( 'All Portfolio Types', 'text-domain' ),
			'parent_item' => null,
			'parent_item_colon' => null,
			'edit_item' => __( 'Edit Portfolio Type', 'text-domain' ),
			'update_item' => __( 'Update Portfolio Type', 'text-domain' ),
			'add_new_item' => __( 'Add New Portfolio Type', 'text-domain' ),
			'new_item_name' => __( 'New Portfolio Type Name', 'text-domain' ),
			'separate_items_with_commas' => __( 'Separate Portfolio Types with commas', 'text-domain' ),
			'add_or_remove_items' => __( 'Add or remove Portfolio Types', 'text-domain' ),
			'choose_from_most_used' => __( 'Choose from the most used Portfolio Types', 'text-domain' )
		);

	$args = array(
		'hierarchical' => true,
		'labels' => $labels,
		'show_ui' => true,
		'query_var' => true,
		'rewrite' => array( 'slug' => 'postfolio_type' )
	);
	register_taxonomy('postfolio_type', 'portfolio', $args);
}

After you paste the code above into your theme’s functions.php, you will see a portfolio menu in the admin panel of your website.

scshot101