Skip to content

Registering taxonomies

John Blackbourn edited this page Jun 6, 2021 · 8 revisions

The register_extended_taxonomy() is a wrapper for WordPress' own register_taxonomy() function, which means that any parameters which are accepted by register_taxonomy() are accepted.

The function's signature looks like this:

function register_extended_taxonomy( string $taxonomy, $object_type, array $args = [], array $names = [] ) : Extended_Taxonomy

Need a simple taxonomy with no frills? You can register a taxonomy with two parameters:

register_extended_taxonomy( 'location', 'post' );

Try it. You'll have a hierarchical public taxonomy with an admin UI, and all the labels and term updated messages will be automatically generated.

Tip: Use a singular name for your taxonomy name, such as location instead of locations.

For a bit more functionality:

register_extended_taxonomy( 'genre', 'story', array(

	# Use radio buttons in the meta box for this taxonomy on the post editing screen:
	'meta_box' => 'radio',

	# Show this taxonomy in the 'At a Glance' dashboard widget:
	'dashboard_glance' => true,

	# Add a custom column to the admin screen:
	'admin_cols' => array(
		'updated' => array(
			'title'       => 'Updated',
			'meta_key'    => 'updated_date',
			'date_format' => 'd/m/Y'
		),
	),

), array(

	# Override the base names used for labels:
	'singular' => 'Genre',
	'plural'   => 'Genres',
	'slug'     => 'story-genre'

) );

Bam, we have a 'Stories' taxonomy attached to the Post post type, with correctly generated labels and term updated messages, radio buttons in place of the standard meta box for this taxonomy on the post editing screen, a custom column in the admin area (you need to handle the term meta population yourself), and a count of the terms in this taxonomy in the 'At a Glance' dashboard widget.

Default arguments for custom taxonomies.

Several of these differ from the defaults in WordPress' register_taxonomy() function.

'public'            => true,  
'show_ui'           => true,  
'hierarchical'      => true,  
'query_var'         => true,  
'exclusive'         => false, # Custom arg  // true means: just one can be selected  
'allow_hierarchy'   => false, # Custom arg  //  
'meta_box'          => null,  # Custom arg  // can be null, 'simple', 'radio', 'dropdown' -> 'radio' and 'dropdown' just allow exclusive choices (will overwrite the set choise), simple has exclusive and multi options  
'dashboard_glance'  => false, # Custom arg  // show or not on dashboard glance  
'checked_ontop'     => null,  # Custom arg  //   
'admin_cols'        => null,  # Custom arg  // added admin columns  
'required'          => false, # Custom arg  //