webdev_wp
webdev_wp

Reputation: 33

get_term_link throws WP_ERROR

I am retrieving the term link using get_term_link and echoing it in a href (link).

It works on the website but when I crawl the website it throws the status code 500.
I have discovered this when it appeared in the php_errors.log on the server.

Error: PHP Catchable fatal error: Object of class WP_Error could not be converted to string in ..etc/etc/

$get_term = get_term_by('slug', 'term', 'taxonomy');
$link = get_term_link($get_term->term_id, 'taxonomy'); 

How can I avoid this crawling issue? Your feedback is highly appreciated, thanks

Upvotes: 1

Views: 2702

Answers (1)

Dmitry
Dmitry

Reputation: 45

Please check this url. https://developer.wordpress.org/reference/functions/get_term_link/#return

(string|WP_Error) HTML link to taxonomy term archive on success, WP_Error if term does not exist.

get_term_link() function returns (string|WP_Error).

Because you provided a bit code. Here are my possible solutions.

  • get_term_link() first parameter not only accepts term id but also term object. so I suggest to change this

    $link = get_term_link($get_term->term_id, 'taxonomy'); 
    

    to

    $link = get_term_link($get_term, 'taxonomy'); 
    
  • Check this function returns exact value.

    $get_term = get_term_by('slug', 'term', 'taxonomy');
    

Hope these help.

Thanks

Upvotes: 1

Related Questions