Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Issue: I have the term_taxonomy_id, I do not have the taxonomy name, I want to retrieve the term object without doing a direct database query.

Thoughts: Well the API docs for get_term_by say that taxonomy is required and the wp_term_taxonomy table schema makes it pretty simple to get the taxonomy if I have the term_taxonomy_id so this should be pretty simple -- however I don't seem to see any API calls that can achieve this.

I could just call the database using something like:

select taxonomy from wp_term_taxonomy where term_taxonomy_id={ID}

But would prefer not to do a direct query to the database. Is there an API call that can achieve this? or a better way to get the term object from the term_taxonomy_id?

Thanks

share|improve this question

3 Answers 3

I was pretty sure of this, but the answer is—at least for now—a definitive no. There isn't an API call for it.

To double-check, I asked core developer Andrew Nacin about this on Twitter. His response:

term_taxonomy_id is, for the moment, essentially private. You should likely never have it to use it.

It might be worth posting a second question about whatever you're doing or at least expanding on the particular situation that leads to this scenario, since your current problem is rather rare—and maybe unnecessary—in the first place. Again from Nacin:

I honestly don't know when you'd ever have term_taxonomy_id and not also already be in a position to know term_id and taxonomy.


Addendum: It should also be noted that the taxonomy schema and meta relationships are slated to undergo some changes, so this may be a particularly dangerous time to try something outside the API.

share|improve this answer
    
The use case for this is the business wants to be able to select a primary taxonomy term across multiple taxonomies. So I am extending the Walker_Category and adding a radio button across each of the term lists for the selected taxonomy. Sorry, hit enter prematurely. Since term_id is not taxonomy/term_id specific, I was looking for a way to guarantee I am getting the right taxonomy/term. My (HACKY) workaround is to make the value comma delimited taxonomy, id. –  setterGetter Jul 3 at 16:38
    
@setterGetter, note the addendum above. –  mrwweb Jul 3 at 17:58
    
Exactly why I don't want to go the direct query route. –  setterGetter Jul 3 at 18:04
    
Interesting addendum, but seeing this: »A disclaimer: This post doesn’t promise anything. Do not rely on the contents of this post for future projects.« It probably really means, could happen or not, sometime or never. @setterGetter –  ialocin Jul 3 at 20:58
    
@ialocin That's true though there is some work getting done. For instance, the blocking ticket mentioned (#17689) is slated for 4.0 and there are some other taxonomy-related improvements as well including #5034 (filed with WordPress 2.3!!!). –  mrwweb Jul 4 at 15:01

@mrwweb gave the right answer here I think and you should consider his objections.

But if you want that functionality, then you have to implement the API call yourself. Which like you said is pretty easy to do:

// create your own API call
function get_taxonomy_by_term_taxonomy_id( $term_taxonomy_id = '' ) {
    global $wpdb;
    if ( empty( $term_taxonomy_id ) ) {
        return false;
    }
    $taxonomy =
        $wpdb->get_row(
            $wpdb->prepare(
                "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d LIMIT 1",
                $term_taxonomy_id
            )
        );
    return $taxonomy;
}

If you are thinking about using the hooks - get_term and get_$taxonomy - available inside the get_term_by() function, its not worth it, because you'll have to make second database query that way.

share|improve this answer
1  
Any reason for %s instead of %d in the prepare? –  Schism Jul 3 at 18:58
    
Nah, besides it happend no reason... @Schism –  ialocin Jul 3 at 20:42

EDIT Just noticed the original question asked for the taxonomy, not the term.

Going to leave this here in case it helps anyone in the future though

Here is my home grown solution. Just stick this in your functions.php file...

if( ! function_exists('get_term_by_term_taxonomy_id') )
{
    function get_term_by_term_taxonomy_id( $term_taxonomy_id = null )
    {
        global $wpdb;

        $res = $wpdb->get_row( 
            $wpdb->prepare(
                'select term_id, taxonomy from {$wpdb->prefix}_term_taxonomy where term_taxonomy_id = %d',
                (int) $term_taxonomy_id
            )
        );

        if( is_wp_error($res) ) return false;

        return get_term_by('id', $res->term_id, $res->taxonomy);
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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