Taxonomy API Functions

Core functions for taxonomy and term registration, retrieval, and management.

Source: wp-includes/taxonomy.php


Taxonomy Registration

register_taxonomy()

Creates or modifies a taxonomy object.

php
register_taxonomy( string $taxonomy, array|string $object_type, array|string $args = array() ): WP_Taxonomy|WP_Error

Note: Do not use before the init hook.

ParameterTypeDescription
$taxonomystringTaxonomy key. Max 32 characters, lowercase alphanumeric, dashes, underscores
$object_typearray|stringObject type(s) to associate with
$argsarray|stringConfiguration arguments

Args

KeyTypeDefaultDescription
labelsstring[][]Labels array. See get_taxonomy_labels()
descriptionstring''Short descriptive summary
publicbooltrueWhether intended for public use
publicly_queryablebool(inherits public)Whether publicly queryable
hierarchicalboolfalseWhether hierarchical (like categories)
show_uibool(inherits public)Whether to show admin UI
show_in_menubool(inherits show_ui)Whether to show in admin menu
show_in_nav_menusbool(inherits public)Whether available in nav menus
show_in_restboolfalseWhether to include in REST API
rest_basestring$taxonomyREST API base path
rest_namespacestring'wp/v2'REST API namespace
rest_controller_classstring'WP_REST_Terms_Controller'REST controller class
show_tagcloudbool(inherits show_ui)Whether to show in tag cloud widget
show_in_quick_editbool(inherits show_ui)Whether to show in quick/bulk edit
show_admin_columnboolfalseWhether to show column on post type listing
meta_box_cbbool|callablenullMeta box callback. false for no meta box
meta_box_sanitize_cbcallable(auto)Meta box sanitization callback
capabilitiesstring[][]Capability mappings
rewritebool|arraytrueRewrite rules configuration
query_varstring|bool$taxonomyQuery var name. false to disable
update_count_callbackcallable(auto)Count update callback
default_termstring|arraynullDefault term configuration
sortbool|nullnullWhether to maintain term order
argsarraynullDefault args for wp_get_object_terms()

Capabilities Array

KeyDefaultDescription
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 Array

KeyDefaultDescription
slug$taxonomyPermastruct slug
with_fronttruePrepend with front base
hierarchicalfalseHierarchical URL structure
ep_maskEP_NONEEndpoint mask

Returns: WP_Taxonomy on success, WP_Error on failure.

Example:

php
add_action( 'init', function() {
    register_taxonomy( 'genre', array( 'book', 'movie' ), array(
        'labels'            => array(
            'name'          => __( 'Genres' ),
            'singular_name' => __( 'Genre' ),
        ),
        'hierarchical'      => true,
        'show_in_rest'      => true,
        'show_admin_column' => true,
        'rewrite'           => array( 'slug' => 'genre' ),
    ) );
} );

unregister_taxonomy()

Unregisters a taxonomy. Cannot unregister built-in taxonomies.

unregister_taxonomy( string $taxonomy ): true|WP_Error
ParameterTypeDescription
$taxonomystringTaxonomy name

Returns: true on success, WP_Error on failure.


register_taxonomy_for_object_type()

Adds an already registered taxonomy to an object type.

register_taxonomy_for_object_type( string $taxonomy, string $object_type ): bool
ParameterTypeDescription
$taxonomystringTaxonomy name
$object_typestringObject type name

Returns: true on success, false if taxonomy or object type doesn’t exist.


unregister_taxonomy_for_object_type()

Removes an already registered taxonomy from an object type.

unregister_taxonomy_for_object_type( string $taxonomy, string $object_type ): bool
ParameterTypeDescription
$taxonomystringTaxonomy name
$object_typestringObject type name

Returns: true on success, false on failure.


Taxonomy Retrieval

get_taxonomy()

Retrieves the taxonomy object.

get_taxonomy( string $taxonomy ): WP_Taxonomy|false
ParameterTypeDescription
$taxonomystringTaxonomy name

Returns: WP_Taxonomy object or false if not found.


get_taxonomies()

Retrieves a list of registered taxonomy names or objects.

php
get_taxonomies( array $args = array(), string $output = 'names', string $operator = 'and' ): string[]|WP_Taxonomy[]
ParameterTypeDescription
$argsarrayKey/value arguments to match against taxonomy objects
$outputstring'names' or 'objects'
$operatorstring'and' or 'or' for matching logic

Returns: Array of taxonomy names or WP_Taxonomy objects.

Example:

php
// Get all public taxonomies
$public_taxonomies = get_taxonomies( array( 'public' => true ), 'objects' );

// Get taxonomies for posts
$post_taxonomies = get_object_taxonomies( 'post', 'objects' );

get_object_taxonomies()

Returns taxonomies registered for the requested object or object type.

get_object_taxonomies( string|string[]|WP_Post $object_type, string $output = 'names' ): string[]|WP_Taxonomy[]
ParameterTypeDescription
$object_typestring|string[]|WP_PostObject type name(s) or post object
$outputstring'names' or 'objects'

Returns: Array of taxonomy names or objects.


taxonomy_exists()

Determines whether the taxonomy name exists.

taxonomy_exists( string $taxonomy ): bool
ParameterTypeDescription
$taxonomystringTaxonomy name

Returns: true if exists, false otherwise.


is_taxonomy_hierarchical()

Determines whether the taxonomy object is hierarchical.

is_taxonomy_hierarchical( string $taxonomy ): bool
ParameterTypeDescription
$taxonomystringTaxonomy name

Returns: true if hierarchical, false if not or taxonomy doesn’t exist.


is_taxonomy_viewable()

Determines whether a taxonomy is considered "viewable".

is_taxonomy_viewable( string|WP_Taxonomy $taxonomy ): bool
ParameterTypeDescription
$taxonomystring|WP_TaxonomyTaxonomy name or object

Returns: true if publicly queryable.


get_taxonomy_labels()

Builds an object with all taxonomy labels out of a taxonomy object.

get_taxonomy_labels( WP_Taxonomy $tax ): object
ParameterTypeDescription
$taxWP_TaxonomyTaxonomy object

Returns: Object with label properties.


Term Retrieval

get_terms()

Retrieves the terms in a given taxonomy or list of taxonomies.

php
get_terms( array|string $args = array(), array|string $deprecated = '' ): WP_Term[]|int[]|string[]|string|WP_Error
ParameterTypeDescription
$argsarray|stringQuery arguments. See WP_Term_Query::__construct()
$deprecatedarray|stringDeprecated. Legacy argument format

Returns: Array of terms, count as numeric string, or WP_Error.

Example:

php
$terms = get_terms( array(
    'taxonomy'   => 'category',
    'hide_empty' => false,
    'orderby'    => 'name',
    'order'      => 'ASC',
) );

get_term()

Gets all term data from database by term ID.

get_term( int|WP_Term|object $term, string $taxonomy = '', string $output = OBJECT, string $filter = 'raw' ): WP_Term|array|WP_Error|null
ParameterTypeDescription
$termint|WP_Term|objectTerm ID or object
$taxonomystringTaxonomy name (optional since 4.4.0)
$outputstringOBJECT, ARRAY_A, or ARRAY_N
$filterstringSanitization filter. See sanitize_term_field()

Returns: WP_Term or array on success, WP_Error or null on failure.


get_term_by()

Gets all term data from database by term field and data.

get_term_by( string $field, string|int $value, string $taxonomy = '', string $output = OBJECT, string $filter = 'raw' ): WP_Term|array|false
ParameterTypeDescription
$fieldstring'slug', 'name', 'term_id'/'id'/'ID', or 'term_taxonomy_id'
$valuestring|intValue to search for
$taxonomystringTaxonomy name. Optional if $field is 'term_taxonomy_id'
$outputstringOBJECT, ARRAY_A, or ARRAY_N
$filterstringSanitization filter

Returns: WP_Term or array on success, false on failure.

Example:

$term = get_term_by( 'slug', 'uncategorized', 'category' );

get_term_children()

Merges all term children into a single array of their IDs.

get_term_children( int $term_id, string $taxonomy ): array|WP_Error
ParameterTypeDescription
$term_idintTerm ID to get children of
$taxonomystringTaxonomy name

Returns: Array of term IDs or WP_Error.


get_term_field()

Gets sanitized term field.

get_term_field( string $field, int|WP_Term $term, string $taxonomy = '', string $context = 'display' ): string|int|null|WP_Error
ParameterTypeDescription
$fieldstringTerm field to fetch
$termint|WP_TermTerm ID or object
$taxonomystringTaxonomy name
$contextstringSanitization context

Returns: Field value or WP_Error.


term_exists()

Determines whether a taxonomy term exists.

term_exists( int|string $term, string $taxonomy = '', int $parent_term = null ): mixed
ParameterTypeDescription
$termint|stringTerm ID, slug, or name
$taxonomystringTaxonomy name
$parent_termintParent term ID to confine search

Returns: null if not found, term ID if no taxonomy specified, array with term_id and term_taxonomy_id if taxonomy specified.


is_term_publicly_viewable()

Determines if a term is publicly viewable.

is_term_publicly_viewable( int|WP_Term $term ): bool
ParameterTypeDescription
$termint|WP_TermTerm ID or object

Returns: true if taxonomy is viewable.


Object-Term Relationships

wp_get_object_terms()

Retrieves the terms associated with the given object(s), in the supplied taxonomies.

php
wp_get_object_terms( int|int[] $object_ids, string|string[] $taxonomies, array|string $args = array() ): WP_Term[]|int[]|string[]|string|WP_Error
ParameterTypeDescription
$object_idsint|int[]Object ID(s) to retrieve terms for
$taxonomiesstring|string[]Taxonomy name(s)
$argsarray|stringSee WP_Term_Query::__construct()

Returns: Array of terms or WP_Error.

Example:

php
$categories = wp_get_object_terms( $post_id, 'category', array(
    'fields' => 'names',
) );

wp_set_object_terms()

Creates term and taxonomy relationships.

wp_set_object_terms( int $object_id, string|int|array $terms, string $taxonomy, bool $append = false ): array|WP_Error
ParameterTypeDescription
$object_idintObject to relate to
$termsstring|int|arrayTerm slug(s) or ID(s). Empty array removes all
$taxonomystringTaxonomy name
$appendboolIf false, replaces existing terms

Returns: Array of term taxonomy IDs or WP_Error.

Example:

php
// Set categories for a post
wp_set_object_terms( $post_id, array( 'news', 'featured' ), 'category' );

// Add tags without replacing existing
wp_set_object_terms( $post_id, array( 'wordpress', 'tutorial' ), 'post_tag', true );

wp_add_object_terms()

Adds term(s) associated with a given object.

wp_add_object_terms( int $object_id, string|int|array $terms, array|string $taxonomy ): array|WP_Error
ParameterTypeDescription
$object_idintObject ID
$termsstring|int|arrayTerm slug(s) or ID(s)
$taxonomyarray|stringTaxonomy name

Returns: Array of term taxonomy IDs or WP_Error.


wp_remove_object_terms()

Removes term(s) associated with a given object.

wp_remove_object_terms( int $object_id, string|int|array $terms, string $taxonomy ): bool|WP_Error
ParameterTypeDescription
$object_idintObject ID
$termsstring|int|arrayTerm slug(s) or ID(s)
$taxonomystringTaxonomy name

Returns: true on success, false or WP_Error on failure.


wp_delete_object_term_relationships()

Unlinks the object from the taxonomy or taxonomies.

wp_delete_object_term_relationships( int $object_id, string|array $taxonomies ): void
ParameterTypeDescription
$object_idintObject ID
$taxonomiesstring|arrayTaxonomy name(s)

is_object_in_term()

Determines if the given object is associated with any of the given terms.

is_object_in_term( int $object_id, string $taxonomy, int|string|int[]|string[] $terms = null ): bool|WP_Error
ParameterTypeDescription
$object_idintObject ID
$taxonomystringTaxonomy name
$termsint|string|arrayTerm ID(s), name(s), or slug(s). null checks any term

Returns: true/false or WP_Error.


is_object_in_taxonomy()

Determines if the given object type is associated with the given taxonomy.

is_object_in_taxonomy( string $object_type, string $taxonomy ): bool
ParameterTypeDescription
$object_typestringObject type string
$taxonomystringTaxonomy name

Returns: true if associated.


get_objects_in_term()

Retrieves object IDs of valid taxonomy and term.

php
get_objects_in_term( int|int[] $term_ids, string|string[] $taxonomies, array|string $args = array() ): string[]|WP_Error
ParameterTypeDescription
$term_idsint|int[]Term ID(s)
$taxonomiesstring|string[]Taxonomy name(s)
$argsarray|stringorder can be 'ASC' or 'DESC'

Returns: Array of object IDs as strings or WP_Error.


Term Modification

wp_insert_term()

Adds a new term to the database.

php
wp_insert_term( string $term, string $taxonomy, array|string $args = array() ): array|WP_Error
ParameterTypeDescription
$termstringTerm name
$taxonomystringTaxonomy name
$argsarray|stringConfiguration arguments

Args

KeyTypeDefaultDescription
alias_ofstring''Slug of term to make this an alias of
descriptionstring''Term description
parentint0Parent term ID
slugstring''Term slug (auto-generated if empty)

Returns: Array with term_id and term_taxonomy_id or WP_Error.

Error Codes:

  • invalid_taxonomy — Taxonomy doesn’t exist
  • invalid_term_id — Term ID is 0
  • empty_term_name — Empty term name
  • missing_parent — Parent term doesn’t exist
  • term_exists — Term already exists at this level

Example:

php
$result = wp_insert_term( 'Jazz', 'genre', array(
    'description' => 'Jazz music genre',
    'slug'        => 'jazz',
    'parent'      => $music_term_id,
) );

if ( ! is_wp_error( $result ) ) {
    $term_id = $result['term_id'];
}

wp_update_term()

Updates term based on arguments provided.

php
wp_update_term( int $term_id, string $taxonomy, array $args = array() ): array|WP_Error
ParameterTypeDescription
$term_idintTerm ID
$taxonomystringTaxonomy name
$argsarraySame as wp_insert_term() args

Returns: Array with term_id and term_taxonomy_id or WP_Error.


wp_delete_term()

Removes a term from the database.

php
wp_delete_term( int $term, string $taxonomy, array|string $args = array() ): bool|int|WP_Error
ParameterTypeDescription
$termintTerm ID
$taxonomystringTaxonomy name
$argsarray|stringConfiguration arguments

Args

KeyTypeDescription
defaultintDefault term ID to assign orphaned objects
force_defaultboolForce default assignment even if object has other terms

Returns: true on success, false if term doesn’t exist, 0 if default category, WP_Error on failure.


wp_delete_category()

Deletes one existing category.

wp_delete_category( int $cat_id ): bool|int|WP_Error
ParameterTypeDescription
$cat_idintCategory term ID

Returns: Same as wp_delete_term().


Term Metadata

add_term_meta()

Adds metadata to a term.

add_term_meta( int $term_id, string $meta_key, mixed $meta_value, bool $unique = false ): int|false|WP_Error
ParameterTypeDescription
$term_idintTerm ID
$meta_keystringMetadata name
$meta_valuemixedMetadata value (must be serializable if non-scalar)
$uniqueboolWhether to prevent duplicate keys

Returns: Meta ID on success, false on failure, WP_Error if term is shared.


get_term_meta()

Retrieves metadata for a term.

get_term_meta( int $term_id, string $key = '', bool $single = false ): mixed
ParameterTypeDescription
$term_idintTerm ID
$keystringMeta key. Empty returns all meta
$singleboolWhether to return single value

Returns: Meta value(s), empty string/array for non-existing, false for invalid term ID.


update_term_meta()

Updates term metadata.

update_term_meta( int $term_id, string $meta_key, mixed $meta_value, mixed $prev_value = '' ): int|bool|WP_Error
ParameterTypeDescription
$term_idintTerm ID
$meta_keystringMetadata key
$meta_valuemixedMetadata value
$prev_valuemixedPrevious value to match for update

Returns: Meta ID if new, true on update, false on failure/no change, WP_Error if term is shared.


delete_term_meta()

Removes metadata matching criteria from a term.

delete_term_meta( int $term_id, string $meta_key, mixed $meta_value = '' ): bool
ParameterTypeDescription
$term_idintTerm ID
$meta_keystringMetadata name
$meta_valuemixedMetadata value to match (optional)

Returns: true on success, false on failure.


register_term_meta()

Registers a meta key for terms.

register_term_meta( string $taxonomy, string $meta_key, array $args ): bool
ParameterTypeDescription
$taxonomystringTaxonomy name. Empty for all taxonomies
$meta_keystringMeta key to register
$argsarrayArguments. See register_meta()

Returns: true on success, false on failure.


unregister_term_meta()

Unregisters a meta key for terms.

unregister_term_meta( string $taxonomy, string $meta_key ): bool
ParameterTypeDescription
$taxonomystringTaxonomy name
$meta_keystringMeta key to unregister

Returns: true on success, false if not registered.


has_term_meta()

Gets all meta data, including meta IDs, for the given term ID.

has_term_meta( int $term_id ): array|false
ParameterTypeDescription
$term_idintTerm ID

Returns: Array with meta_key, meta_value, meta_id, term_id or false.


Term Counting

wp_count_terms()

Counts how many terms are in taxonomy.

php
wp_count_terms( array|string $args = array(), array|string $deprecated = '' ): string|WP_Error
ParameterTypeDescription
$argsarray|stringQuery arguments. See WP_Term_Query::__construct()
$deprecatedarray|stringDeprecated. Legacy format

Returns: Numeric string count or WP_Error.


wp_update_term_count()

Updates the amount of terms in taxonomy.

wp_update_term_count( int|array $terms, string $taxonomy, bool $do_deferred = false ): bool
ParameterTypeDescription
$termsint|arrayTerm taxonomy ID(s)
$taxonomystringTaxonomy name
$do_deferredboolWhether to flush deferred counts

Returns: true on success, false if empty.


wp_defer_term_counting()

Enables or disables term counting.

wp_defer_term_counting( bool $defer = null ): bool
ParameterTypeDescription
$deferbooltrue to enable deferring, false to disable and flush

Returns: Whether deferring is enabled.


Term Hierarchy

term_is_ancestor_of()

Checks if a term is an ancestor of another term.

term_is_ancestor_of( int|object $term1, int|object $term2, string $taxonomy ): bool
ParameterTypeDescription
$term1int|objectPotential parent term
$term2int|objectChild term
$taxonomystringTaxonomy name

Returns: true if $term2 is child of $term1.


get_ancestors()

Gets an array of ancestor IDs for a given object.

get_ancestors( int $object_id = 0, string $object_type = '', string $resource_type = '' ): int[]
ParameterTypeDescription
$object_idintObject ID
$object_typestringPost type or taxonomy name
$resource_typestring'post_type' or 'taxonomy'

Returns: Array of ancestor IDs from lowest to highest.


wp_get_term_taxonomy_parent_id()

Returns the term’s parent’s term ID.

wp_get_term_taxonomy_parent_id( int $term_id, string $taxonomy ): int|false
ParameterTypeDescription
$term_idintTerm ID
$taxonomystringTaxonomy name

Returns: Parent term ID or false.


Term URLs

Generates a permalink for a taxonomy term archive.

get_term_link( WP_Term|int|string $term, string $taxonomy = '' ): string|WP_Error
ParameterTypeDescription
$termWP_Term|int|stringTerm object, ID, or slug
$taxonomystringTaxonomy name

Returns: URL string or WP_Error.

Example:

$category = get_category_by_slug( 'news' );
$link = get_term_link( $category );

Sanitization

sanitize_term()

Sanitizes all term fields.

sanitize_term( array|object $term, string $taxonomy, string $context = 'display' ): array|object
ParameterTypeDescription
$termarray|objectTerm to check
$taxonomystringTaxonomy name
$contextstring'raw', 'edit', 'db', 'display', 'rss', 'attribute', 'js'

Returns: Term with sanitized fields.


sanitize_term_field()

Sanitizes the field value in the term based on the context.

sanitize_term_field( string $field, string $value, int $term_id, string $taxonomy, string $context ): mixed
ParameterTypeDescription
$fieldstringTerm field to sanitize
$valuestringField value
$term_idintTerm ID
$taxonomystringTaxonomy name
$contextstringSanitization context

Returns: Sanitized field value.


wp_unique_term_slug()

Makes term slug unique, if it isn’t already.

wp_unique_term_slug( string $slug, object $term ): string
ParameterTypeDescription
$slugstringProposed slug
$termobjectTerm object

Returns: Unique slug.


Caching

clean_term_cache()

Removes all of the term IDs from the cache.

clean_term_cache( int|int[] $ids, string $taxonomy = '', bool $clean_taxonomy = true ): void
ParameterTypeDescription
$idsint|int[]Term ID(s)
$taxonomystringTaxonomy name
$clean_taxonomyboolWhether to clean taxonomy-wide caches

clean_object_term_cache()

Removes the taxonomy relationship to terms from the cache.

clean_object_term_cache( int|array $object_ids, array|string $object_type ): void
ParameterTypeDescription
$object_idsint|arrayObject ID(s)
$object_typearray|stringObject type(s)

clean_taxonomy_cache()

Cleans the caches for a taxonomy.

clean_taxonomy_cache( string $taxonomy ): void
ParameterTypeDescription
$taxonomystringTaxonomy slug

get_object_term_cache()

Retrieves the cached term objects for the given object ID.

get_object_term_cache( int $id, string $taxonomy ): bool|WP_Term[]|WP_Error
ParameterTypeDescription
$idintObject ID
$taxonomystringTaxonomy name

Returns: Array of WP_Term objects, false if not cached, WP_Error on error.


update_object_term_cache()

Updates the cache for the given term object ID(s).

update_object_term_cache( string|int[] $object_ids, string|string[] $object_type ): void|false
ParameterTypeDescription
$object_idsstring|int[]Object ID(s)
$object_typestring|string[]Object type(s)

Returns: void on success, false if all already cached.


update_term_cache()

Updates terms in cache.

update_term_cache( WP_Term[] $terms, string $taxonomy = '' ): void
ParameterTypeDescription
$termsWP_Term[]Array of term objects
$taxonomystringNot used

update_termmeta_cache()

Updates metadata cache for list of term IDs.

update_termmeta_cache( array $term_ids ): array|false
ParameterTypeDescription
$term_idsarrayList of term IDs

Returns: Array of metadata or false if nothing to update.


wp_lazyload_term_meta()

Queue term meta for lazy-loading.

wp_lazyload_term_meta( array $term_ids ): void
ParameterTypeDescription
$term_idsarrayList of term IDs

Display Functions

the_taxonomies()

Displays the taxonomies of a post with available options.

php
the_taxonomies( array $args = array() ): void
ParameterTypeDescription
$argsarrayDisplay arguments

Args

KeyTypeDefaultDescription
postint|WP_Post0Post ID or object
beforestring''Output before taxonomies
sepstring' 'Separator between taxonomies
afterstring''Output after taxonomies
templatestring'%s: %l.'Label/terms template
term_templatestring'<a href="%1$s">%2$s</a>'Single term template

get_the_taxonomies()

Retrieves all taxonomies associated with a post.

php
get_the_taxonomies( int|WP_Post $post = 0, array $args = array() ): string[]
ParameterTypeDescription
$postint|WP_PostPost ID or object
$argsarrayDisplay arguments

Returns: Array of formatted taxonomy strings keyed by taxonomy name.


get_post_taxonomies()

Retrieves all taxonomy names for the given post.

get_post_taxonomies( int|WP_Post $post = 0 ): string[]
ParameterTypeDescription
$postint|WP_PostPost ID or object

Returns: Array of taxonomy names.


Split Terms

wp_get_split_terms()

Gets data about terms that previously shared a single term_id, but have since been split.

wp_get_split_terms( int $old_term_id ): array
ParameterTypeDescription
$old_term_idintOld, pre-split term ID

Returns: Array of new term IDs, keyed by taxonomy.


wp_get_split_term()

Gets the new term ID corresponding to a previously split term.

wp_get_split_term( int $old_term_id, string $taxonomy ): int|false
ParameterTypeDescription
$old_term_idintOld, pre-split term ID
$taxonomystringTaxonomy name

Returns: New term ID or false.


wp_term_is_shared()

Determines whether a term is shared between multiple taxonomies.

wp_term_is_shared( int $term_id ): bool
ParameterTypeDescription
$term_idintTerm ID

Returns: true if shared, false if not or splitting is finished.