Getting the parent from the wp_term_taxonomy

My experience with this was that it is an issue area of the taxonomy system in core. In several cases the core code does fetch info based on a tt_id or an array of them, but in each of these cases it is painfully done with a custom SQL query inside that particular function rather than an API function.

To solve my own problem I wrote a simple helper function that should probably be in core. It doesn’t fetch a specific field but instead the entire term_taxonomy row (including ->parent). Ultimately this is no slower than getting a single field and it is a very fast query due to checking for a specific ID.

/**
 * Fetch the term_taxonomy data for a tt_id from the term_taxonomy table
 *
 * Should be in core but isn't. May need review after updates
 * Returns full full row of data but nothing from actual terms table.
 *
 * @global object $wpdb
 * @param integer $tt_id term_taxonomy_id who's row you want to fetch.
 * @return object Database row object for tt_id (term_id, name, slug, taxonomy, description, parent, count)
 */
function gv_get_term_taxonomy_details($tt_id) {
    global $wpdb;
    $tt_details_results = $wpdb->get_results("SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = ($tt_id)");
    $tt_details = $tt_details_results[0];

    return $tt_details;
}