Opened 16 years ago
Closed 15 years ago
#10277 closed enhancement (duplicate)
How to get root category
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 2.9 |
Component: | Taxonomy | Keywords: | root category |
Focuses: | Cc: |
Description ¶
I have looking for some function that allow me to know whick is the root category of one given category when we have a category tree.
I found only how to get the parent category, but not the root, so I made this function for one of my projects:
function get_category_root($category_id){ $category = $category_id->cat_ID; global $wpdb; $parent = $wpdb->get_var( "SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '$category'"); if ($parent == 0) { return $category; } while ($parent != 0){ $category = $parent; $parent = $wpdb->get_var( "SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '$category'"); if ($parent == 0) { return $category; }else{ $category = $parent; } } }
Maybe it's useful.
Pull Requests
- Loading…
Change History (12)
#2
@
16 years ago
It's working in my very concrete installation with its special features. But, of course, it had a lot of fails.
Thanks a lot! I'm testing it.
And agree with the root ancestor function.
#6
follow-up:
↓ 7
@
15 years ago
I like the Idea to have such a function but as said, this needs to be implemented properly. At least endless recursion must be successfully prevented. Database should be taken into considerations to not make such a function more expensive than it must be.
#7
in reply to:
↑ 6
;
follow-up:
↓ 8
@
15 years ago
Replying to hakre:
I like the Idea to have such a function but as said, this needs to be implemented properly. At least endless recursion must be successfully prevented. Database should be taken into considerations to not make such a function more expensive than it must be.
I have used filosofo's implementation in .diff. I think it prevents endless recursion and database problems. What do you think?
#8
in reply to:
↑ 7
@
15 years ago
Replying to bi0xid:
I have used filosofo's implementation in .diff. I think it prevents endless recursion and database problems. What do you think?
It will create and endless loop given the following data:
Cat1->parent = Cat2 Cat2->parent = Cat3 Cat3->parent = Cat1
calling get_category_root($cat_id =
id of Cat1)
will create an endless loop.
Maybe that gives you an idea. What I like is that it resolves linear so far.
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:
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.