WP_Taxonomy

Core class used for interacting with taxonomies. Represents a registered taxonomy and its configuration.

Source: wp-includes/class-wp-taxonomy.php
Since: 4.7.0

Class Declaration

#[AllowDynamicProperties]
final class WP_Taxonomy

Properties

PropertyTypeVisibilityDescription
$namestringpublicTaxonomy key
$labelstringpublicName shown in the menu. Usually plural
$labelsstdClasspublicLabels object for this taxonomy
$descriptionstringpublicShort descriptive summary. Default ''
$publicboolpublicWhether intended for public use. Default true
$publicly_queryableboolpublicWhether publicly queryable. Default true
$hierarchicalboolpublicWhether hierarchical (like categories). Default false
$show_uiboolpublicWhether to generate admin UI. Default true
$show_in_menuboolpublicWhether to show in admin menu. Default true
$show_in_nav_menusboolpublicWhether available in navigation menus. Default true
$show_tagcloudboolpublicWhether to list in Tag Cloud Widget. Default true
$show_in_quick_editboolpublicWhether to show in quick/bulk edit. Default true
$show_admin_columnboolpublicWhether to show column on post type listing. Default false
$meta_box_cbbool|callablepublicMeta box display callback. Default null
$meta_box_sanitize_cbcallablepublicMeta box sanitization callback
$object_typestring[]publicArray of object types registered for
$capstdClasspublicCapabilities object for this taxonomy
$rewritearray|falsepublicRewrites information
$query_varstring|falsepublicQuery var string
$update_count_callbackcallablepublicCount update callback
$show_in_restboolpublicWhether to include in REST API. Default false
$rest_basestring|boolpublicREST API base path
$rest_namespacestring|boolpublicREST API namespace. Default 'wp/v2'
$rest_controller_classstring|boolpublicREST controller class name
$rest_controllerWP_REST_ControllerpublicLazily computed REST controller instance
$default_termarray|stringpublicDefault term configuration
$sortbool|nullpublicWhether to maintain term order. Default null
$argsarray|nullpublicDefault args for wp_get_object_terms()
$_builtinboolpublicWhether it is a built-in taxonomy

Static Properties

PropertyTypeVisibilityDescription
$default_labels(string|null)[][]protectedCached default labels array

Methods

__construct()

Constructor. Instantiates a taxonomy object.

php
public function __construct( string $taxonomy, array|string $object_type, array|string $args = array() )
ParameterTypeDescription
$taxonomystringTaxonomy key. Must not exceed 32 characters
$object_typearray|stringObject type(s) for the taxonomy
$argsarray|stringConfiguration arguments. See register_taxonomy()

Note: Do not instantiate directly. Use register_taxonomy().


set_props()

Sets taxonomy properties.

php
public function set_props( string|string[] $object_type, array|string $args ): void
ParameterTypeDescription
$object_typestring|string[]Object type name(s)
$argsarray|stringConfiguration arguments

Filters Applied:

  • register_taxonomy_args — Filters registration arguments
  • register_{$taxonomy}_taxonomy_args — Filters arguments for specific taxonomy

Processing:

  1. Parses and applies default arguments
  2. Sets publicly_queryable from public if not specified
  3. Configures query_var and rewrite rules
  4. Inherits show_ui, show_in_menu, show_in_nav_menus, show_tagcloud, show_in_quick_edit from defaults
  5. Sets default capabilities
  6. Configures meta box callbacks
  7. Processes default_term configuration
  8. Generates labels via get_taxonomy_labels()

add_rewrite_rules()

Adds the necessary rewrite rules for the taxonomy.

php
public function add_rewrite_rules(): void

Behavior:

  • Adds query var if query_var is set and not in admin
  • Adds rewrite tag and permastruct if rewrite is configured
  • Uses hierarchical tag (.+?) or non-hierarchical ([^/]+) based on settings

remove_rewrite_rules()

Removes any rewrite rules, permastructs, and rules for the taxonomy.

php
public function remove_rewrite_rules(): void

Behavior:

  • Removes query var from $wp->query_vars
  • Removes rewrite tag and permastruct

add_hooks()

Registers the ajax callback for the meta box.

php
public function add_hooks(): void

Behavior:

Adds filter wp_ajax_add-{$taxonomy} pointing to _wp_ajax_add_hierarchical_term.


remove_hooks()

Removes the ajax callback for the meta box.

php
public function remove_hooks(): void

Behavior:

Removes filter wp_ajax_add-{$taxonomy}.


get_rest_controller()

Gets the REST API controller for this taxonomy.

php
public function get_rest_controller(): WP_REST_Controller|null

Returns: Controller instance, or null if show_in_rest is false.

Behavior:

  • Returns null if show_in_rest is falsy
  • Uses rest_controller_class if set, otherwise WP_REST_Terms_Controller
  • Validates class exists and extends WP_REST_Controller
  • Lazily instantiates and caches controller

get_default_labels()

Returns the default labels for taxonomies.

php
public static function get_default_labels(): (string|null)[][]

Returns: Associative array of label keys to arrays of [non-hierarchical, hierarchical] values.

Label Keys:

KeyNon-HierarchicalHierarchical
nameTagsCategories
singular_nameTagCategory
search_itemsSearch TagsSearch Categories
popular_itemsPopular Tagsnull
all_itemsAll TagsAll Categories
parent_itemnullParent Category
parent_item_colonnullParent Category:
name_field_description(shared)(shared)
slug_field_description(shared)(shared)
parent_field_descriptionnull(description)
desc_field_description(shared)(shared)
edit_itemEdit TagEdit Category
view_itemView TagView Category
update_itemUpdate TagUpdate Category
add_new_itemAdd TagAdd Category
new_item_nameNew Tag NameNew Category Name
separate_items_with_commasSeparate tags with commasnull
add_or_remove_itemsAdd or remove tagsnull
choose_from_most_usedChoose from the most used tagsnull
not_foundNo tags found.No categories found.
no_termsNo tagsNo categories
filter_by_itemnullFilter by category
items_list_navigationTags list navigationCategories list navigation
items_listTags listCategories list
most_usedMost UsedMost Used
back_to_items← Go to Tags← Go to Categories
item_linkTag LinkCategory Link
item_link_descriptionA link to a tag.A link to a category.

reset_default_labels()

Resets the cache for the default labels.

php
public static function reset_default_labels(): void

Behavior:

Clears self::$default_labels to force regeneration on next access.


Capabilities Object

The $cap property contains:

PropertyDefaultDescription
manage_terms'manage_categories'Manage terms capability
edit_terms'manage_categories'Edit terms capability
delete_terms'manage_categories'Delete terms capability
assign_terms'edit_posts'Assign terms capability

Rewrite Configuration

When $rewrite is an array:

KeyTypeDefaultDescription
slugstring$taxonomyURL slug
with_frontbooltruePrepend with front base
hierarchicalboolfalseHierarchical URL structure
ep_maskintEP_NONEEndpoint mask

Usage Example

php
// Taxonomy is created via register_taxonomy(), not directly
add_action( 'init', function() {
    register_taxonomy( 'genre', 'book', array(
        'labels' => array(
            'name' => 'Genres',
            'singular_name' => 'Genre',
        ),
        'hierarchical' => true,
        'show_in_rest' => true,
        'rewrite' => array( 'slug' => 'book-genre' ),
    ) );
} );

// Get taxonomy object
$taxonomy = get_taxonomy( 'genre' );

// Access properties
echo $taxonomy->name;           // 'genre'
echo $taxonomy->labels->name;   // 'Genres'
echo $taxonomy->hierarchical;   // true

// Get REST controller
$controller = $taxonomy->get_rest_controller();