1

I've a really strange issue with Wordpress and the get_the_link()-function and a following issue with objects who are objects, but identified as non-object-error, itself they are working.

First the get_term_link() Issue:

If I ask with a normal number and taxonomy, it works well: $tax_name = 'products'; print get_term_link(13, $tax_name);

It returns the url, but if I do it like following: $tax_id = (int)$navItem['id']; $tax_name = 'products'; print get_term_link($tax_id , $tax_name);

I got the error-message:

Catchable fatal error: Object of class WP_Error could not be converted to string in /home/username/www/domainname/wp-content/themes/theme/sidebar-left.php on line 89

Which last line was 89. ( var_dump of tax_id was int(13) )

Without the (int) before the navItem['id] var_dump resulted in string(2).

Tried this with a get_term_by()-function as source, too. $term = get_term_by('id', $navItem['id'], $tax_name); $tax_id = $term->term_id; var_dump($tax_id); $tax_name = 'products'; print get_term_link($tax_id , $tax_name);

Got a new error message as additional :

Notice: Trying to get property of non-object in /home/username/www/domainname/wp-content/themes/theme/sidebar-left.php on line 88

What's wrong, because line 88 is in this example "$tax_id = $term->term_id;"! I can use (int) but this don't change the error-message.

The funny thing is, the $term variable is an object. And this error message explains me that I try to get a property from a non-object. If I convert this object to an array (get_object_vars()-function), and access the values in the array like an array (name[key])

I get the following error message:

get_object_vars() expects parameter 1 to be object, null given line 86

I tried to convert them to integer, then to string. Tried to use slug instead of ID. But nothing worked. The results working!

Yeah! All showed lines returning the correct link-url!

But I get this error messages... and I don't understand why. Because... they are totally illogical for me. But I think or more, I hope that I'm the illogical part of this issue and someone can help me to solve/understand this.

And NO... I don't like to turn/hide the error-messages.

1

get_term_link() will return a WP_Error object on error.

You can't print an object or array or you will get messages similar to what you are getting. Use var_dump() instead or print_r() and you should see what is occuring. Your error even tells you that WP_Error is the object involved.

You are getting errors when the term you are looking for is not found.

And yes, you should hide your error messages on a production site. They can be revealing. Write to a log if you must have errors on the production site.

  • I know that and the contents of the objects/array I everytimes checking by var_dump() and print_ar(). – Sascha Apr 16 '15 at 6:16
  • (Additional comment-text, because of 5 min Edit limit) To turn debug-modus and error-messages on a production-site, I already know too. But at the moment the page is on a test-server and there I've the error-messages for development. And what i meant with the last line was, that I don't like the answers/comments who recommending you to hide simply the error messages. (understandable that I will publish later a stable and error-free version of the theme) – Sascha Apr 16 '15 at 6:22
  • Sorry, if this doesn't answer your question then I don't know what the question is. – s_ha_dum Apr 16 '15 at 16:05
  • No, you don't have too. The question was, why I got this error-messages, because I don't understood them. Talking from non-object related to an object was to curious. But I solved it now... or more worked around of it. (specify ARRAY_A as output format of the get_terms_by()-function to get a clean array) – Sascha Apr 17 '15 at 10:52

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.