Get used terms by an author as array of strings

I don’t know which line that error refers to but this code works correctly if everything falls into place exactly right.

But there are actually several ways this could go wrong. For example, if get_query_var('author_name') is not set you get an error on this line:

$used_terms=list_author_used_terms($user->ID);

If a problem occurs here:

$terms = wp_get_object_terms( $p->ID, 'wossom');

then you get and error here:

$author_terms[] = (string)$t->name;

Because $t will be a WP_Error object and not the stdClassobject with term data that you expect.

Something very similar happens with this line:

<a href="https://wordpress.stackexchange.com/questions/102894/<?php echo get_term_link( $ut,"wossom"); ?>"><?php echo  $ut; ?></a>

I think the solution is to get into a habit of verifying that you have the kind of data you think you have before you try to use that data.

For example:

foreach ($used_terms as $ut) {
  $link = get_term_link( $ut,"category");
  if (!is_wp_error($link)) {
    ?>
    <a href="https://wordpress.stackexchange.com/questions/102894/<?php echo $link; ?>"><?php echo  $ut; ?></a>
    <?php
  }
}

Leave a Comment