WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 15 months ago

Last modified 15 months ago

#21378 closed enhancement (invalid)

get_the_category doesn't work for custom post types with custom taxonomies

Reported by: tcattitude Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.4.1
Component: Taxonomy Keywords:
Focuses: Cc:

Description

I just got this problem and figured out a workaround.
So i think i should post this here so devs can see if they can solve it in a more decent way.

As title says, get_the_category fails if you ask for a custom post type WITH a custom taxonomy too (a combination of both).

It's simple to reproduce.
Just register a new post type, in my case was "videos". Then put a custom taxonomy for them too (my case, "categoria", spanish for category).
(ref how to: http://new2wp.com/pro/wordpress-custom-post-types-and-taxonomies-done-right/, codex, etc ).
As this pic: http://i.imm.io/xVdN.png

Put some posts on that new custom post type with the new taxonomy.

Then, you can try to call get_the_category inside a loop, our outside it passing the post ID get_the_category(ID) and it will fail.

Outside the loop, like this:

$postid = '33';
$catinfo = get_the_category($postid);
var_dump($catinfo);

You only get an empty array:

array(0) { }

Into the loop, same history.

And i think it's because custom taxonomy it's not "category" (the default).
The problematic funcion has "category" as taxonomy hardcoded in the first line:
http://wpseek.com/get_the_category/
It says:

$categories = get_the_terms( $id, 'category' );

I was able to "fix" this, for me, just cloning that function in my theme's function.php, and changing that line to hardcode my custom taxonomy in there:

$categories = get_the_terms( $id, 'categoria' );

Then my custom get_the_category, with a postid supposed to be in that taxonomy, works.

Obviously it's not the ideal workaround.
I think the proper should be get_the_category can search other registered taxonomies too, automatically.
If can't, maybe pass get_the_category a second argument so you can directly specify wich taxonomy should get_the_terms search for inside the function.

Change History (7)

comment:1 @ocean903 years ago

  • Keywords close added; 2nd-opinion needs-testing removed

Since get_the_category is only for categories, like the name says, I'm not sure what you want.

The right way is get_the_terms( $id, 'categoria' ).

comment:2 @nacin3 years ago

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

comment:3 @tcattitude3 years ago

  • Keywords 2nd-opinion dev-feedback added; close removed
  • Resolution invalid deleted
  • Status changed from closed to reopened
  • Type changed from defect (bug) to enhancement

Maybe i just post this as an enhancement instead.

I talking about this:
http://wordpress.org/support/topic/get-the-category-not-working-for-custom-post-type?replies=5#post-3013294

function get_the_category_bytax( $id = false, $tcat = 'category' ) {
    $categories = get_the_terms( $id, $tcat );
    if ( ! $categories )
        $categories = array();

    $categories = array_values( $categories );

    foreach ( array_keys( $categories ) as $key ) {
        _make_cat_compat( $categories[$key] );
    }

    // Filter name is plural because we return alot of categories (possibly more than #13237) not just one
    return apply_filters( 'get_the_categories', $categories );
}

But for the default "get_the_category".

Hope this can be reconsidered.

comment:4 @helenyhou3 years ago

Is there some reason get_the_terms() doesn't work for you? Or, for that matter, enabling the default category taxonomy to be used with your custom post type? get_the_category() is specifically for the category taxonomy, not your custom taxonomy that is similarly named.

comment:5 @nacin3 years ago

  • Keywords 2nd-opinion dev-feedback removed
  • Resolution set to fixed
  • Status changed from reopened to closed

A post type is "post", "page", "book"

A taxonomy is "tag", "category", "author"

A term is "programming reference", "Non-Fiction", "Andrew Nacin"

get_the_category() will work on a post and return Non-Fiction, which is a category assigned to that post. (To put it another way, it is a term in the taxonomy of category.)

If you want to get the terms for a custom taxonomy for a post, don't use get_the_category(). Use get_the_terms(), specifying the taxonomy you'd like.

comment:6 follow-up: @leroy-b15 months ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

@ Nacin: "If you want to get the terms for a custom taxonomy for a post, don't use get_the_category(). Use get_the_terms(), specifying the taxonomy you'd like."

– I still don´t understand. what is a possible resolution, if what I want is exactly the standard 'category' to be listed in a custom post type? (this is what seems the concern of tcattitude, after all)

comment:7 in reply to: ↑ 6 @SergeyBiryukov15 months ago

  • Resolution set to invalid
  • Status changed from reopened to closed

Replying to leroy-b:

what is a possible resolution, if what I want is exactly the standard 'category' to be listed in a custom post type?

Use register_taxonomy_for_object_type() or the taxonomies argument of register_post_type() to add the default category taxonomy (or a custom taxonomy) to a custom post type.

Use get_the_category() to retrieve categories assigned to the post (from the default category taxonomy).

Use get_the_terms() to retrieve other terms assigned to the post (from custom taxonomies).

Please try the support forums if you have more questions: http://wordpress.org/support/.

Last edited 15 months ago by SergeyBiryukov (previous) (diff)
Note: See TracTickets for help on using tickets.