Object of class WP_Error could not be converted to string

get_term can return a WP_Error object in addition to a falsy value for term not found or an actual term row.

You fix this, by adding an additional check:

if (!$term) {
   continue;
}

Becomes:

if (!$term || is_wp_error($term)) {
    continue;
}

You should also do this above the get_term_link call.

$term = get_term($value, $fieldSettings['taxonomy']);
if (!$term || is_wp_error($term)) {
    continue;
}

$link = get_term_link($term);

get_term usually returns a WP_Error when the taxonomy doesn’t exist or isn’t registered (you can look at the source for more info). So much sure that it is. If you are registering it, make sure that the above code (that’s causing the error) is happening sometime after init, where the taxonomy is probably registered.

Leave a Comment