0

What I need to do is to :

-+ Retrieve all terms assigned to a page -+ Get all posts assigned to each term -+ Organize the output in the hierarchic way as in the admin

I'm OK with the first two items, but I cannot seems to find a way to organize my output in the correct hierarchical manner (probably because i'm not good with arrays and probably PHP in general).

This is what I got for now :

public function return_all_tax_objects() {

    $obj_term_posts = array();

    foreach ( $this->return_object_terms() as $obj_term ) {

        if ( $obj_term->parent ) {
            unset( $key );
            $ancestors = array_reverse( get_ancestors( $obj_term->term_id, $obj_term->taxonomy ) );
            foreach ( $ancestors as $ancestor ) {
                $ancestor_term = get_term( $ancestor, $obj_term->taxonomy );
                $key .= "[$ancestor_term->slug]";
            }
            $key .= "[$obj_term->slug]";
        }

        if ( array_key_exists( $key, $obj_term_posts ) ) {

             $get_term_posts = $this->get_posts_for_term( $obj_term );
             array_push ( $obj_term_posts[ $key ], $this->get_posts_for_term( $obj_term ) );

        } else {

            $obj_term_posts[ $key ] = $this->get_posts_for_term( $obj_term );

        }

    }
    return $obj_term_posts;
}

And $this->return_object_terms() corresponds to a function to returns all terms for a ID for all Custom Post Types I have set up, in a flat array :

public function return_object_terms() {

    return wp_get_object_terms( $this->object_id, $this->return_object_taxonomies(), array ( 'orderby' => 'term_id', 'order' => 'ASC', 'fields' => 'all' ) );
}

As you will see in my snapshot for the returned values, I'm close, but :

Edit : Changed code for a better option. Still, not working.

Somehow, I got it all but mixed up. I have the correct posts, but I'm not updating the correct index somwhere.

Essentially, what I need is a way to nest child terms into parent term, so I could structure my output :

-+ Direction générale des services
--+ (posts)
---+ Services Techniques
---+ (posts)
----+ Directeur des Services Techniques
----+ (posts)
---+ Services Administratifs
---+ (posts)
...

Firebug capture of returned array $obj_term_posts

0

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.