Getting the parent from the wp_term_taxonomy

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

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

Example

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

Related posts

Leave a Reply

3 comments

  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;
    }
    
  2. The below code will create a column on the user table (users.php) and display the name of a taxonomy term. This is for you if you had users with an assigned custom role. The database will only display the id in an object, but this code will extract the name from the ID and display it on the users table:

    <?php
    // Display column on user table
    add_filter('manage_users_columns', 'column_register_acf_name');
    
    add_filter('manage_users_custom_column', 'column_display_acf_name', 10, 3);
    
    function column_register_acf_name($columns)
    {
        $columns['id_col'] = 'ID Name';
        return $columns;
    }
    
    //Adds Content To The Custom Added Column
    function get_names($term_id)
    {
        global $wpdb;
        $result = $wpdb->get_var($wpdb->prepare("SELECT name FROM $wpdb->terms WHERE term_id = %d", $term_id));
        return $result;
    }
    
    
    function column_display_acf_name($value, $column_name, $user_id)
    {
        $user_info = get_user_meta($user_id, 'gatekeeper', true);
    
        $ints = array_map('intval', $user_info);
        $names = array_map('get_names', $ints);
        if ($column_name == 'id_col') return implode(', ', $names);
        return implode($value);
    }