0

I encountered a problem trying to make a select button comprising options of custom post type taxonomies (terms, taxonomies, not sure about proper nomenclature?).

So, the function:

$terms = get_terms(array(
    "hide_empty" => false,
));

foreach ($terms as $term) {

    echo "<li>".$term->name."</li>";
}

Whenever I call the function without stating "taxonomy" parameter (plain function, as above) it's working fine displaying all Wordpress taxonomies including those for my custom post type.

When I include "taxonomy" => "category" in get_terms() arguments array it gives me list of native post categories, for "taxonomy" => "post_tag" it displays all pos tags names but when I change it to "taxonomy" => "my_custom_taxonomy" (which is being displayed just fine among other taxonomies when I don't include "taxonomy" parameter at all, just as I mentioned above) it returns Notice: Trying to get property of non-object in C:\XAMPP\htdocs\........php on line xx

Why is it happening and is there a simple workaround to this?

All help will be much apprieciated! Thanks.

  • Without seeing the full error, it's hard to say, but keep in mind that the code assumes terms were found and never checks if $terms is empty or an error object – Tom J Nowell Apr 14 '17 at 19:45
  • Sorry, I didn't highlight the full error. Dots in the error is just the path to .php file and two xs are the line number where the error occurs. – Daniel Apr 14 '17 at 19:56
  • What's in that file at that location? You shouldn't trivialise/ignore PHP warnings and notices when trying to debug things, you're being given a huge clue there! Include the full path, and line number, and what's on the line it mentions – Tom J Nowell Apr 14 '17 at 20:40
  • @TomJNowell It's a path to file and line where the above foreach loop echoes $term->name, there you go: Notice: Trying to get property of non-object in C:\XAMPP\htdocs\archeonew\wp-content\themes\test_theme\inc\admin\meta\profiles_template_meta.php on line 23 and the warning is being displayed twice, one under another. – Daniel Apr 14 '17 at 21:30
  • Can you check that $terms is indeed an array of terms and not an empty array of a WP_Error object? empty() and is_wp_error( $terms ) should do the trick – Tom J Nowell Apr 16 '17 at 3:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.