Home Forums General Issues Auto select parent taxonomy term


Auto select parent taxonomy term

  • Is there a way to do this? Or when listing the terms, how can you query if the term has a parent?

  • 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;
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Auto select parent taxonomy term’ is closed to new replies.