[wp-trac] [WordPress Trac] #21378: get_the_category doesn't work for custom post types with custom taxonomies
WordPress Trac
wp-trac at lists.automattic.com
Wed Jul 25 15:36:49 UTC 2012
#21378: get_the_category doesn't work for custom post types with custom taxonomies
--------------------------+---------------------------------------
Reporter: tcattitude | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Taxonomy | Version: 3.4.1
Severity: normal | Keywords: 2nd-opinion needs-testing
--------------------------+---------------------------------------
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.
--
Ticket URL: <http://core.trac.wordpress.org/ticket/21378>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list