Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active July 10, 2018 07:15
Show Gist options
  • Save tommcfarlin/475f3d28451809ffd43e5ffcb686e05d to your computer and use it in GitHub Desktop.
Save tommcfarlin/475f3d28451809ffd43e5ffcb686e05d to your computer and use it in GitHub Desktop.
[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