Why does get_term() require taxonomy? Are term_ids not unique?

A related WPSE question asks how to get the term by specifying ID only, without specifying taxonomy. My question is more philosophical. Generally, stuff in WP core is there for a reason. I’m trying to understand why term_id can’t be the primary key for the term – why do we need the taxonomy as well? Can a single term record be a member of multiple taxonomies? That’s certainly not currently supported in the API. Is there a use case where this might be desirable?

Or is the required $taxonomy parameter in get_term() a vestigial tail from an earlier incarnation of the database structure?

Related posts

Leave a Reply

2 comments

  1. I’ve logged a ticket against this with trac: http://core.trac.wordpress.org/ticket/20536

    However, it turns out that for the time being it IS necessary, as WordPress currently (since 2.x) has a bug that DOES associate two terms with the same name to the same term_id! So it IS possible (though incorrect) for a single term to be associated with more than one taxonomy. See this bug: http://core.trac.wordpress.org/ticket/5809

    It’s pretty wide-reaching so implementing the fix will need to be unit-tested very thoroughly. I’ll try to remember to update this question if there are any developments.

  2. why do we need the taxonomy as well? Can a single term record be a member of multiple taxonomies?

    No. Terms have slugs to support term archives. And having the slugs two times brings up a lot of minetraps or “wontfix”es in some permalink scenarios.

    Is there a use case where this might be desirable?

    Yes: Imagine that you have terms for e.g. size and have three taxonomies named length, width and height (you could also think about colors). But that’s not supported for the reasons written above.

    Generally, stuff in WP core is there for a reason. I’m trying to understand why term_id can’t be the primary key for the term – why do we need the taxonomy as well?

    From looking at the table and running an EXPLAIN, term_id is the primary key. I guess on larger systems it might be faster to only query taxonomy specific terms using a (slow) join than querying all, sorting them and filtering what you need.