1
    function im_check_term($name,$tax){
        $term =  get_term_by("name", $name,$tax);
        return !is_wp_error($term) ? $term->term_id : false;
    }

Notice: Trying to get property of non-object in /home/pcodecom/demo.p30code.com/multimedia-2/wp-content/plugins/imdb/imdb.php on line 11

2 Answers 2

0

If you look at the documentation for get_term_by(), you'll see that it:

Will return false if $taxonomy does not exist or $term was not found.

You need to account for this possibility in your code by checking the value of $term. You'll also note from the documentation that get_term_by() does not return a WP_Error, so is_wp_error() is not useful. This is what you need:

$term = get_term_by( 'name', $name, $tax );

return $term ? $term->term_id : false;

The specific error you're seeing is because if $term is false then $term->term_id is invalid code.

0

get_term_by() might return the boolean value false. So the simple check for a WP_Error is not sufficient. Use the instanceof operator instead and test for a WP_Term object:

function im_check_term($name,$tax){
    $term =  get_term_by("name", $name,$tax);
    return ( $term instanceof WP_Term ) ? $term->term_id : false;
}

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.