Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] Get Taxonomy By Term ID
<?php
/**
* Retrieves the taxonomy name associated on the specified $term_id.
*
* @access private
* @param int $term_id The term ID from which to retrieve the taxonomy name.
* @return string $taxonomy The name of the taxaonomy associated with the term ID.
*/
private function get_taxonomy_by_term_id( $term_id ) {
// We can't get a term if we don't have a term ID.
if ( 0 === $term_id || null === $term_id ) {
return;
}
// Grab the term using the ID then read the name from the associated taxonomy.
$taxonomy = '';
$term = get_term( $term_id );
if ( false !== $term ) {
$taxonomy = $term->taxonomy;
}
return trim( $taxonomy );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment