Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught Error: Cannot use object of type WP_Term as array

I had this issue since I updated my WordPress, in my website I have a custom post which contains some custom categories, something like that:

1) Parent category: Food | Child: Fries, Hamburger, Maple Syrup…

2) Parent category: Year | Child: 2016, 2015, 2014…

3) Parent category: Country | Child: USA, Canada, Spain…

So when I'm writting my custom post, I would chose within those categories and just choose (tickle the box) the category that I need. And it would shown something like that:

Title: New recipe

Content: My text

Terms: Food: Mapple Syrup / Country: Canada / Year: 2014

But now, the terms doesn't show at all, and I get this error message: Cannot use object of type WP_Term as array

I used to have the following PHP code which allowed to me to retrieve the child category's parent (and used it as a prefix) and also allowed to me change the order.

  $term_list = wp_get_post_terms($post->ID, 'project_cat', array("fields" => "all"));
                            $terms_hierarchy = array();
                            foreach ($term_list as $term_single) {
                                $parent = $term_single->parent;
                                if ($parent != 0) {
                                    $terms_hierarchy[$parent][] = get_term($parent)->slug;
                                    $terms_hierarchy[$parent]['children'][$term_single->term_id] = $term_single->name;
                                } else {
                                    $terms_hierarchy[$parent] = $term_single;
                                }
                            }
   //PHP indicated this line:
                            foreach ($terms_hierarchy as $key => $term) {
                                echo "<span>$term[0]: </span>";
                                if (!empty($term['children'])) {
                                    $s_children = '';
                                    foreach ($term['children'] as $key => $child) {
                                        if ($term[0] == 'client') {
                                            $tax_meta = get_term_meta($key);
                                            if(!empty($tax_meta['external_url'][0])){
                                               $s_children .= "<a target='_blank' href='{$tax_meta['external_url'][0]}'>$child</a>, ";
                                            }
                                            else {
                                                $s_children .= $child . ', ';
                                            }
                                        }
                                        else {
                                            $s_children .= $child . ', ';
                                        }
                                    }
                                    echo rtrim($s_children, ', ') . "<br />";
                                }
                            }

I'll be really grateful if someone can help to figure out what went wrong?

Thanks for your time

like image 969
cbtr Avatar asked Jan 20 '17 17:01

cbtr


1 Answers

$terms_hierarchy is not an array it is an Std Object. So ...

$term['children']

... would actually be ...

$term->children

like image 81
AlexBet Avatar answered Sep 21 '22 00:09

AlexBet