1

Is there a built in wordpress function that let's me look up the "parent" field of the wp_term_taxonomy table if I pass the TT_ID to it?

Something like

get_value ( $wp_table_name, $id_fieldname, $id_value,
$the_field_info_to_retrieve )

Example

get_value ( "wp_term_taxonomy", "term_taxonomy_id", "parent");
1

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;
}
0

Try this-

$term_id = 4; // term id
$taxonomy = 'category'; // Taxonomy name that $term_id is part of
$term_data = get_term( $term_id, $taxonomy );
$parent = $term_data->parent; // this return the parent of given term id

Ref: https://codex.wordpress.org/Function_Reference/get_term

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.