Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using list_author_used_terms function below to get the terms used by a specific author (terms of a custom taxonomy 'wossom' for a custom post type 'aya-bi-aya').

function list_author_used_terms($author_id){

    $posts = get_posts( array('post_type' => 'aya-bi-aya', 'posts_per_page' => -1, 'author' => $author_id) );

    $author_terms = array();

    foreach ($posts as $p) {
        $terms = wp_get_object_terms( $p->ID, 'wossom');

        foreach ($terms as $t) {
            $author_terms[] = (string)$t->name;
        }

    }
    return array_unique($author_terms);
}

In author.php template file, I want to display the terms used by the given author. I do like this:

<?php
$user = get_user_by('login',get_query_var('author_name'));

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

foreach ($used_terms as $ut) {
?>
<a href="<?php echo get_term_link( $ut,"wossom"); ?>"><?php echo  $ut; ?></a>
<?php
} 
?>

When I print_r the $used_terms, I get the correct array, but the foreach loop is displaying nothing.

I get the following error when I try to print seperately get_term_link( $ut[0],"wossom"); :

Catchable fatal error: Object of class WP_Error could not be converted to string in C:\xampp\htdocs\blog\wp-content\themes\twentyten\author.php on line

I know it is a pure php problem but I am really tired trying to resolve it. Your help is appreciated.

share|improve this question
    
Why are you trying to use $ut[0] as array, when your individual items in $used_terms seem to be strings, if I follow code right? –  Rarst Jun 13 '13 at 20:09
    
get_term_link first parameter should be string, which is not the case because $used_terms is not an array of strings. using $ut[0] is only for testing. I hope I am not wrong. –  whiteletters and blankspaces Jun 13 '13 at 20:11
    
Why isn't it array of string if your list.. function seem to return precisely that ( $author_terms[] = (string)$t->name; )? –  Rarst Jun 13 '13 at 20:15
    
This is the illogical thing that is making me blocked. I didnt have at the beginning the (string), and even after adding it, my problem is not resolved. –  whiteletters and blankspaces Jun 13 '13 at 20:17
1  
Please add that to the question. By now your code is quoted differently in place error happens and you say that data is not in a form that code suggests it is. –  Rarst Jun 13 '13 at 20:29

2 Answers 2

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="<?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="<?php echo $link; ?>"><?php echo  $ut; ?></a>
    <?php
  }
}
share|improve this answer
    
I thank you for giving me the the useful advice fo using '!is_wp_error'. When I used it for $link, the remaining part of author.php executed, which means that get_term_link ( $ut,"category"); is returning WP_error even if $used_terms is looking as stdClass when displayed. When I do !is_wp_error for $t variable, nothing change ($used_terms stay the same) Thank you S_HA_DUM for your time –  whiteletters and blankspaces Jun 14 '13 at 9:38
1  
Like I said, this works if everything lines up, but there are numerous places it could go wrong. You need to debug step by step to find all the points of failure. –  s_ha_dum Jun 14 '13 at 14:14
    
I am here to emphasize on what you said. In addition, last time I was not understanding what does var_dump do (suggested as debug method by Rarst). After using it I now see that my array fields are always strings. and now I can detect at each step what is the format of my variables. I think (I am not sure yet) that what is returning WP_error is get_term_link since this function requires the first parameter to be term_id, term_slug, but not the term_name. I thank you very much for your time, I will be back here to post the solution. Thank you for your time. –  whiteletters and blankspaces Jun 16 '13 at 18:29
    
Thank you for your time, I have posted an answer. –  whiteletters and blankspaces Jun 17 '13 at 19:32
up vote 1 down vote accepted

I learned from Rarst and S_ha_dum two new nice techniques that I want to share with everybody beginner in coding PHP/Wordpress:

  • Var_dump function is useful to check what is the nature of a variable during the steps of code.

  • geting into the habit of verifying that you have the kind of data you think you have before you try to use that data. For example, performing if (!is_wp_error($variable)) {} when you read from Codex that $variable contains the return of a function that can be WP_Error.

More details can be found in comments and S_ha_dum answer.

Solution to my problem:

What was returning WP_error is get_term_link function which requires the first parameter to be term_id or term_slug, and not term_name. But I want to notice that only the term_slug worked for me not the term_id even Codex say this.

The code became:

foreach ($used_terms as $ut) {
  $inter= get_term_by( 'name',$ut,'wossom' );
  $link = get_term_link($inter->slug,'wossom');

  if (!is_wp_error($link)) {
    ?>
    <a href="<?php echo $link; ?>"><?php echo  $inter->term_id; ?></a>
    <?php
  }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.