Get term name from term ID?

I cannot figure out how to get the taxonomy term name if I have the taxonomy term’s ID.

This is my current code

// create a dropdown menu of the quantity taxonomy 
wp_dropdown_categories( 
    array('taxonomy' => 'quantity_category', 'name' => 'productQuantity', 'hide_empty' => 0)
); 

$quantityTerms = $_POST['productQuantity'];
$quantityTax   = 'quantity_category';

The value of $quantityTerms is not the “name” of the quantity but the ID of the quantity category. When it sets the object terms, it creates a new category called “ID#” and not inserting it into the category by name.

wp_set_object_terms( $post_id, $quantityTerms, $quantityTax, $append );

1

The function get_term_by() would allow you to get the taxonomy term name from the id.

$quantityTermObject = get_term_by( 'id', absint( $quantityTerms ), 'quantity_category' );
$quantityTermName = $quantityTermObject->name;

Leave a Comment