0

I have the following:

foreach( $this_post_terms as $term ) :
  $thiscat = get_term_by( 'slug', $term->slug, 'categories_whatever' );
  echo $thiscat->ID;
endforeach;

The last line throws an error, Undefined property: stdClass::$ID.

What's weird to me is that

echo $thiscat->name;

works, doesn't throw any error.

2

The fields returned by get_term_by() are:

  • term_id
  • name
  • slug
  • term_group
  • term_taxonomy_id
  • taxonomy
  • description
  • parent
  • count

So instead of $thiscat->ID, you should use $thiscat->term_id

Ref: http://codex.wordpress.org/Function_Reference/get_term_by

1

Not weird, there is no ID in a term object, it's term_id. You can also var_dump($thiscat) in your loop to see all of its properties.

  • You beat me to it :) – henrywright Nov 28 '14 at 17:13

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.