Checking return with WP Error

I’ve seen other people with similar problems, and I know that I need to check the return when calling for the child items in my custom taxonomy “hhie_artists”. I’m not sure how to echo my output to incorporate wp_error.
Here is my code:

<?php
$taxonomyName = "hhie_artists";
//Call top layer terms only (by setting Parent to 0).
$parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'slug', 'hide_empty' => true));   
echo '<ul>';
foreach ($parent_terms as $pterm) {
//Get the Child terms
$terms = get_terms($taxonomyName, array('parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => true));
foreach ($terms as $term) {
echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';  
}
}
echo '</ul>';
?>

From what I’ve found out, this is due to an empty taxonomy field being found. I’ve yet to populate all my custom taxonomy fields, so I’m not surprised by this error, I just need to solve it. Thanks.

Related posts

Leave a Reply

1 comment

  1. Not entirely sure what you are asking, but if I understand correctly you want to perform some testing on the return value in order to avoid iterating a WP_Error object and perhaps handle errors in some way.

    According to the documentation get_terms() will return an array of term objects or false if no objects were found, but clearly by looking at the source it will return a WP_Error object if the taxonomy does not exists or in some cases an empty array.

    Thus, to check for errors you will have to check if the return value matches false, empty array (array()) or a WP_Error object. Calling empty($terms) will account for the first two, and is_wp_error($terms) can be used for testing if the return value is an error object. Something like this will work for performing such testing:

    $taxonomyName = "hhie_artists";
    //Call top layer terms only (by setting Parent to 0).
    $parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'slug', 'hide_empty' => true));   
    
    if (!empty($parent_terms) && !is_wp_error($parent_terms)) {
        echo '<ul>';
        foreach ($parent_terms as $pterm) {
            //Get the Child terms
            $terms = get_terms($taxonomyName, array('parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => true));
            if (!empty($terms) && !is_wp_error($terms)) {
                foreach ($terms as $term) {
                    echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';  
                }
            }
        }
        echo '</ul>';
    }
    

    However, there is already a function available in WordPress for printing a category tree like this. It’s called wp_list_categories and i strongly suggest you use that instead. Here’s how:

    wp_list_categories(array(
        'title_li' => null, // We don't need the automatically generated heading
        'depth' => 1, // Judging from your code you only want to print one level of terms
        'orderby' => 'slug',
        'taxonomy' => 'hhie_artists',
    ));
    

    The result should follow this structure:

    • Parent artist A
      • Child artist A1
      • Child artist A2
      • Child artist A3
    • Parent artist B
      • Child artist B1
      • Child artist B2