[wp-trac] Re: [WordPress Trac] #10277: How to get root category

WordPress Trac wp-trac at lists.automattic.com
Fri Jun 26 10:48:28 GMT 2009


#10277: How to get root category
-------------------------+--------------------------------------------------
 Reporter:  bi0xid       |       Owner:  filosofo     
     Type:  enhancement  |      Status:  new          
 Priority:  normal       |   Milestone:  Unassigned   
Component:  Taxonomy     |     Version:  2.9          
 Severity:  normal       |    Keywords:  root category
-------------------------+--------------------------------------------------
Changes (by filosofo):

  * owner:  => filosofo
  * component:  General => Taxonomy


Comment:

 I think the function as above will give you some problems.  First, there
 could be more than one entry for a term_id in term_taxonomy.  You really
 need to specify the taxonomy or (better yet) just let the WordPress API
 handle it for you.  Also, if something is a category ID (an integer), then
 it won't have an object property of "cat_ID", so
 {{{$category_id->cat_ID}}} will probably produce a PHP notice, at least.

 I think this should do the same thing:

 {{{
 function get_category_root($cat_id = 0) {
         $category_parent = get_category($cat_id);
         while( ! empty( $category_parent->category_parent ) ) {
                 $category_parent =
 get_category($category_parent->category_parent);
         }
         return ! empty( $category_parent->term_id ) ?
 $category_parent->term_id : $cat_id;
 }
 }}}

 What would be more useful in my opinion would be a function that, given a
 term ID and a particular taxonomy, returns an array of IDs of that term's
 ancestors, in order.  That way you could easily get the root ancestor, but
 you could also easily check for the presence of any term ancestor.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/10277#comment:1>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list