#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 (8)
#2
@
12 years ago
- Milestone Awaiting Review deleted
- Resolution set to invalid
- Status changed from new to closed
#3
follow-up:
↓ 8
@
12 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.
#4
@
12 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.
#5
@
12 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.
#6
follow-up:
↓ 7
@
11 years 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)
#7
in reply to:
↑ 6
@
11 years 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/.
#8
in reply to:
↑ 3
@
8 years ago
Tcattitude dude you are a genius !! And a life saviour.
Thank you very much for all this !!!! :)
Replying to tcattitude:
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.
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' )
.