Skip to content

Integration with Extended Taxonomies

Olivier Raimbaud edited this page May 7, 2018 · 5 revisions

Extended Taxonomies was a companion library to Extended CPTs which has now been merged into this library.

Usage

register_extended_taxonomy( $taxonomy, $object_type, $args, $names );

Parameters

$taxonomy (string) (required) - The name of the taxonomy. Name should only contain lowercase letters and the underscore character, and not be more than 32 characters long.

$object_type (array|string) (required) - Name(s) of the object type(s) for the taxonomy

Arguments

$args (array) (optional) - The $args parameter accepts all the standard arguments for register_taxonomy() in addition to several custom arguments that provide extended functionality. See the wordpress default arguments here: Register Taxonomy Arguments

'meta_box' (string) - The name of the custom meta box to use on the post editing screen for this taxonomy

  • 'radio' - a meta box with radio inputs
  • 'simple' - a meta box with a simplified list of checkboxes
  • 'dropdown' - a meta box with a dropdown menu
  • 'custom_callback_function' - pass the name of a callback function, eg my_super_meta_box()
  • 'false' - Boolean to remove the meta box completely
  • 'null' (default) - the standard WordPress meta box is used

'checked_ontop' (bool) - Whether to always show checked terms at the top of the meta box. This allows you to override WordPress' default behaviour if necessary.

  • 'false' - Default if using a custom_callback_function in the meta_box arguments
  • 'true' - Default if not using a custom_callback_function in the meta_box arguments

'dashboard_glance' (bool) - Whether to show this taxonomy on the 'At a Glance' section of the admin dashboard. Default false.

'admin_cols' (array) - Associative array of admin screen columns to show for this taxonomy. Admin Columns Documentation

'exclusive' (bool) - This parameter isn't feature complete. All it does currently is set the meta box to the 'radio' meta box, thus meaning any given post can only have one term associated with it for that taxonomy. 'exclusive' isn't really the right name for this, as terms aren't exclusive to a post, but rather each post can exclusively have only one term. It's not feature complete because you can edit a post in Quick Edit and give it more than one term from the taxonomy.

'allow_hierarchy' (bool) - Disable hierarchy in the taxonomy's rewrite rules. Default false;

$names (array) (optional) - The plural, singular, and slug names.

  • 'plural' (string) - The plural form of the taxonomy name.
  • 'singular' (string) - The singular form of the taxonomy name.
  • 'slug' (string) - The slug used in the term permalinks for this taxonomy.

Example:

register_extended_taxonomy( 'book_type', 'book', array(  // register onto custom post type `book`
	'meta_box' => 'simple',
        'checked_ontop' => true,
        'dashboard_glance' => true,
        'admin_cols' => array(
                'title',
		// A meta field column:
		'published' => array(
			'title'       => 'Published',
			'meta_key'    => 'published_date',
			'date_format' => 'd/m/Y'
		),

        ),
        'allow_hierarchy' => false 
),
array(
        'singular' => 'Book Type',
        'plural'   => 'Book Types',
        'slug'     => 'book-types'
) );

Shorthand Usage

You can use a shorthand method to create a custom taxonomy and associate it with your custom post type by using the add_taxonomy() method on the Extended_CPT object that's returned by register_extended_post_type().

Example:

$example = register_extended_post_type( 'example', array(
	'featured_image' => 'Example image',
) );
$example->add_taxonomy( 'foo', array(
	'show_ui' => false,
) );
$example->add_taxonomy( 'bar', array(
	'hierarchical' => false,
) );