Support

Account

Home Forums General Issues Auto select parent taxonomy term Reply To: Auto select parent taxonomy term

  • Hi @rickysullivan,

    Thanks for the post.

    You can check whether the term has a parent by using the following function which returns a parent term:

    function get_term_top_most_parent($term_id, $taxonomy){
        // start from the current term
        $parent  = get_term_by( 'id', $term_id, $taxonomy);
        // climb up the hierarchy until we reach a term with parent = '0'
        while ($parent->parent != '0'){
            $term_id = $parent->parent;
    
            $parent  = get_term_by( 'id', $term_id, $taxonomy);
        }
        return $parent;
    }