Help building array hierarchically with taxonomies (to get posts)

I made it !

This is the final code that returns a hierarchical array based on taxonomy.

public function return_all_tax_objects() {

    $obj_term_posts = array();

            // GET FLAT LIST OF TERMS BELONGING TO THE POST
    foreach ( $this->return_object_terms() as $obj_term ) {

                    // IF TERM HAS A PARENT
        if ( $obj_term->parent ) {
            unset ( $keys );
                            // GET ANCESTORS OF TERM AND REVERSE IT 
                            // SO WE HAVE IT FROM ROOT
            $ancestors = array_reverse( get_ancestors( $obj_term->term_id, $obj_term->taxonomy ) );
                            // WE GET ANCESTORS AND STORE SLUG IN ARRAY
            foreach ( $ancestors as $ancestor ) {
                $ancestor_term = get_term( $ancestor, $obj_term->taxonomy );
                $keys[] = $ancestor_term->slug;
            }
        }

                    // LAST ITEM FOR KEYS IS CURRENT TERM SLUG
        $keys[] = $obj_term->slug;

                    // DUPLICATE WITH INCLUSION 
        $temp =& $obj_term_posts;

                    // CREATE INDEX FROM KEYS ARRAY
        foreach ( $keys as $key ) {
            $temp =& $temp[ $key ];
        }
        // GET ASSOCIATED POSTS TO THAT TERM 
        $temp = $this->get_posts_for_term( $obj_term );

    }

    return $obj_term_posts;
}

Result :
enter image description here

Leave a Comment

error code: 523