WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 4 years ago

#10277 closed enhancement (duplicate)

How to get root category

Reported by: bi0xid Owned by: filosofo
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.

Attachments (1)

category.php.r12462.diff (847 bytes) - added by bi0xid 4 years ago.
Category with get_root_category

Download all attachments as: .zip

Change History (12)

comment:1 filosofo5 years ago

  • Component changed from General to Taxonomy
  • Owner set to filosofo

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.

comment:2 bi0xid5 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.

comment:3 Denis-de-Bernardy5 years ago

  • Milestone changed from Unassigned to 2.9

comment:4 ryan4 years ago

  • Milestone changed from 2.9 to Future Release

bi0xid4 years ago

Category with get_root_category

comment:5 bi0xid4 years ago

Trying to get this solved.

comment:6 follow-up: hakre4 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.

comment:7 in reply to: ↑ 6 ; follow-up: bi0xid4 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?

comment:8 in reply to: ↑ 7 hakre4 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.

comment:9 hakre4 years ago

Related: #8384

comment:10 voyagerfan57614 years ago

  • Cc WordPress@… added

comment:11 scribu4 years ago

  • Milestone Future Release deleted
  • Resolution set to duplicate
  • Status changed from new to closed

See #12908

Note: See TracTickets for help on using tickets.