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 Answers 2

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.

1
  • You beat me to it :) Commented Nov 28, 2014 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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