krozero
krozero

Reputation: 6453

Wordpress get taxonomy name by term id

Is there a way to pull taxonomy name just by using term id? Like i have a term_id and i don't know which taxonomy it belongs to and need to get the taxonomy name that it belongs too. any way?

Thanks!

Upvotes: 11

Views: 41461

Answers (3)

honk31
honk31

Reputation: 4377

to get the labels of a taxonomy by one of its term ids, you would need to first get the term by calling

$term = get_term($term_id);

now you have the term object and that also includes $term->taxonomy. with that info you can call for the whole taxonomy

$taxonomy = get_taxonomy($term->taxonomy);

now you have the whole package and you could extract the name from that.

var_dump($taxonomy);

$label = $taxonomy->label;
$name = $taxonomy->labels->name;
$singular_name = $taxonomy->labels->singular_name;

or you could call another function, that gets you all the labels:

$labels = get_taxonomy_labels($taxonomy);

var_dump($labels);
echo $labels->name;

Upvotes: 2

Ritika Sharma
Ritika Sharma

Reputation: 41

One can get taxanomy name using term id:

In the below example group_types is the custom taxonomy in WordPress

$groupTypeName = get_term_by('term_taxonomy_id','group_types' , 'category');
$tax_name= $groupTypeName->name;
echo $tax_name;

Upvotes: 0

Carl Knapp
Carl Knapp

Reputation: 239

You can use get_term(): https://codex.wordpress.org/Function_Reference/get_term

$term = get_term( $term_id );
echo $term->taxonomy;

Upvotes: 22

Related Questions