WordPress   

Create custom taxonomies in WordPress

This snippet will create two custom taxonomies (Song Category and Song Tag) for the custom post Songs. It can be easily customized according to your needs by just replacing the given texts.

// create two custom taxonomies Song Type and Song Tags for custom post Songs
add_action( 'init', 'create_songs_taxonomies', 0 );

function create_songs_taxonomies() {
	// Add hierarchical taxonomy
	$labels = array(
		'name'              => _x( 'Song Categories', 'taxonomy general name', 'textdomain' ),
		'singular_name'     => _x( 'Song Category', 'taxonomy singular name', 'textdomain' ),
		'search_items'      => __( 'Search Song Categories', 'textdomain' ),
		'all_items'         => __( 'All Song Categories', 'textdomain' ),
		'parent_item'       => __( 'Parent Category', 'textdomain' ),
		'parent_item_colon' => __( 'Parent Category:', 'textdomain' ),
		'edit_item'         => __( 'Edit Category', 'textdomain' ),
		'update_item'       => __( 'Update Category', 'textdomain' ),
		'add_new_item'      => __( 'Add New Song Category', 'textdomain' ),
		'new_item_name'     => __( 'New Category Name', 'textdomain' ),
		'menu_name'         => __( 'Song Category', 'textdomain' ),
	);

	$args = array(
		'hierarchical'      => true,
		'labels'            => $labels,
		'show_ui'           => true,
		'show_admin_column' => true,
		'query_var'         => true,
		'rewrite'           => array( 'slug' => 'song-cat' ),
	);

	register_taxonomy( 'song-category', array( 'songs' ), $args );

	// Add non hierarchical taxonomy
	$labels = array(
		'name'                       => _x( 'Song Tags', 'taxonomy general name', 'textdomain' ),
		'singular_name'              => _x( 'Song Tag', 'taxonomy singular name', 'textdomain' ),
		'search_items'               => __( 'Search Tags', 'textdomain' ),
		'popular_items'              => __( 'Popular Song Tags', 'textdomain' ),
		'all_items'                  => __( 'All Song Tags', 'textdomain' ),
		'parent_item'                => null,
		'parent_item_colon'          => null,
		'edit_item'                  => __( 'Edit Tag', 'textdomain' ),
		'update_item'                => __( 'Update Tag', 'textdomain' ),
		'add_new_item'               => __( 'Add New Song Tag', 'textdomain' ),
		'new_item_name'              => __( 'New Tag', 'textdomain' ),
		'separate_items_with_commas' => __( 'Separate Tags with commas', 'textdomain' ),
		'add_or_remove_items'        => __( 'Add or remove Tags', 'textdomain' ),
		'choose_from_most_used'      => __( 'Choose from the most used Tags', 'textdomain' ),
		'not_found'                  => __( 'No Tags found.', 'textdomain' ),
		'menu_name'                  => __( 'Song Tags', 'textdomain' ),
	);

	$args = array(
		'hierarchical'          => false,
		'labels'                => $labels,
		'show_ui'               => true,
		'show_admin_column'     => true,
		'update_count_callback' => '_update_post_term_count',
		'query_var'             => true,
		'rewrite'               => array( 'slug' => 'song-tag' ),
	);

	register_taxonomy( 'song-tag', array('songs'), $args );
}
Need Professional Support for Your Website Problems?

Whether you're facing website issues or struggling with code implementation, our team is here to assist. Hire us to get your website back on track.

Hire Us