WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#34243 closed enhancement (wontfix)

Make `get_taxonomy()` work like `get_post_type()`

Reported by: flixos90 Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.4
Component: Taxonomy Keywords: dev-feedback 2nd-opinion close
Focuses: Cc:

Description

After term splitting and having finished up #14162, I think it would be useful to add / adjust standardized functions to terms that have been there for posts as well.

We need simple functions for making it easier to work with term IDs independently, just like we can work with post IDs.

In particular, I'm proposing to add a function like get_taxonomy( $term ) that works like get_post_type( $post ) and returns the taxonomy name.

The issue now is that there already exists a function of that name, but it basically works more like get_post_type_object( $post_type ). What I'm thinking is we could modify the get_taxonomy() function to support both kinds of functionalities. In addition to the above, we could introduce a new function get_taxonomy_object( $taxonomy ). The two functions could be something like this:

function get_taxonomy( $term ) {
    // new get_taxonomy() behavior
    if ( is_numeric( $term ) || is_object( $term ) && isset( $term->term_id ) ) {
        if ( $term = get_term( $term ) )
            return $term->taxonomy;
        
        return false;
    }
    
    // old behavior for back compat
    return get_taxonomy_object( $term );
}

function get_taxonomy_object( $taxonomy ) {
    global $wp_taxonomies;
    
    if ( ! taxonomy_exists( $taxonomy ) )
        return false;
 
    return $wp_taxonomies[$taxonomy];
}

At a later stage we could possibly deprecate the old way of using get_taxonomy() so that developers would be recommended to use the new function instead of the old one.

Having one function serve two purposes might be a little confusing, but when we think long-term, this would be an acceptable caveat in my opinion.

Change History (5)

#1 @flixos90
5 years ago

  • Keywords dev-feedback 2nd-opinion added

#2 @DrewAPicture
5 years ago

  • Keywords close added

get_taxonomy() isn't inherently the same concept as retrieving a post type and I'm not sure I'd expect it to return anything other than a taxonomy object.

I've opened #34245 to make the $taxonomy parameter in get_term_field() optional now that $taxonomy is also now optional in get_term().

I think leveraging an existing function like get_term_field() should suffice for this need.

Edit: With the proposed change in #34245, this could be done with:

$taxonomy = get_term_field( 'taxonomy', $term );

Without it (currently):

$taxonomy = get_term_field( 'taxonomy', $term, 'my-taxonomy' );
Last edited 5 years ago by DrewAPicture (previous) (diff)

#3 follow-up: @flixos90
5 years ago

@DrewAPicture:
I was rather asking for a function get_taxonomy() which works like get_post_type() because it would be more standardized and easier to understand I think. Now that term IDs are globally unique, they could basically have similar functions like posts have.
For example we have:

  • get_post() and get_term()
  • get_post_field() and get_term_field()
  • get_post_meta() and get_term_meta()

When I look at the names of those, I immediately get what they do. So when I see function names like get_post_type() and get_taxonomy() I would expect them to have a similar functionality as well, one for posts and the other for terms, but here it isn't the case. The same applies to get_post_type_object() where I would expect that there also exists a function called get_taxonomy_object() that does the same thing for terms, but that function is currently called get_taxonomy().

The issue I have is more about conventions than functionality. The change you proposed in #34245 will indeed allow to get the taxonomy for a term ID with a single function, so that covers the functionality aspect. But wouldn't it be straightforward to structure the function names of term functions in the same way like their post equivalents?

#4 in reply to: ↑ 3 @DrewAPicture
5 years ago

Replying to flixos90:

The issue I have is more about conventions than functionality. The change you proposed in #34245 will indeed allow to get the taxonomy for a term ID with a single function, so that covers the functionality aspect. But wouldn't it be straightforward to structure the function names of term functions in the same way like their post equivalents?

I understand what you're asking for. The primary issue is that get_taxonomy() has been around for a long time, and has default expected behavior we can't change – back-compat rules there.

Even if we added a $term parameter to add an additional return type (the taxonomy name), you'd still have to pass a $taxonomy value to it. I recognize what you're trying to do, but whatever solution we come up with, it's probably not going to be called get_taxonomy().

Best bet is something like get_taxonomy_name(), which would likely just be a wrapper for get_term_field( 'taxonomy', $term ) in the end, which is partially why I opened #34245.

#5 @boonebgorges
5 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

The real problem here is that get_post_type() is a poorly named function. It should be get_post_post_type() - that is, "get the post_type of a post". This better parallels get_post_field(). The corresponding term function would then be get_term_taxonomy().

In any case, it seems to me that wrapper fields like this for get_term_field() are not all that useful. For consistency, we'd want functions for all the properties of the term, but do we really want or need get_term_term_group()? Seems to me that get_term_field(), especially after #34245, is more than enough.

Note: See TracTickets for help on using tickets.