PHP wordpress get a multidimensional array of category objects

am trying to get all the categories on my wordpress as a heirarchial multidimensional array of objects of type category.
There are 2 functions in wordpress that kind of do what I want, but not exactly :

1) get_categories() – This one does return an array of category objects, which is great, but then the array is a flat one, where there is no differentiation between which category is a parent or which one is a child.

Read More

2) wp_list_categories() – which can return the categories with the herarchy intact, but problem is that the return value is html, and I need the categories to still be objects.

For example, if I had the following 3 trees as my parent-less categories :

/*
tree_stump

tree1
    branch1

tree2
    branch2
        twig2
            leaves2
            flowers2
            fruits2
*/

I am trying to get a return value which could be something like:

$cat_tree=array( 1=>$tree_stump,2=>array(1=>$tree1,2=>$branch1),3=>array(1=>$tree2,2=>array(1=>$branch2,2=>array($twig2,$leaves2,$flowers2,$fruits2  ) ) ) );

where each member of the multi dim array is still an object, and in all the child arrays, the first one is the parent of the rest of the members. I guess this would need some kinda recursive stuff – the stuff that gives me jitters !

Not sure if I made sense, please do let me know if I need to be more clear..
Thanks for reading.

Related posts

Leave a Reply

4 comments

  1. Here’s my version which assumes $categories is the output of get_categories() but it doesn’t matter what order they come in.

    $cats_tree = get_cat_tree(0,$categories);
    function get_cat_tree($parent,$categories) {
        $result = array();
        foreach($categories as $category){
            if ($parent == $category->category_parent) {
                $category->children = get_cat_tree($category->cat_ID,$categories);
                $result[] = $category;
            }
        }
        return $result;
    }
    
  2. this is what i did:
    First get an array using get_categories(). Then transform it into a new array.
    I’m assuming that all children are below its parents in the original array.

    $cats_tree = array();
    foreach($categories as $category){
        $parent = $category->parent;
        if ( $parent && isset($cats_tree[$parent]) ){
            $cats_tree[$parent]->children[$category->cat_ID] = $category;
        }else{              
            $cats_tree[$category->cat_ID] = $category;
        }
    }
    

    Hope this is still useful for someone

  3. <?php
    $args = array(
        'taxonomy' => 'category',
        'orderby' => 'name',
        'order' => 'ASC',
        'hierarchical'  => true,
        'hide_empty' => false,
    );
    $the_query = new WP_Term_Query($args);
    $categories = $the_query->get_terms();    
    
    function get_cat_tree($parent,$categories) {
        $result = array();
        foreach($categories as $category){
            if ($parent == $category->parent) {
                $category->children = get_cat_tree($category->term_id,$categories);
                $result[] = $category;
            }
        }
        return $result;
    }
    $cats_tree = get_cat_tree(0,$categories);