Search code examples
wordpresstagstaxonomy

get_terms(get_the_ID) shows WP_Error


Im trying to display my custom taxonomy tags from inside the post but it returns NULL or WP_Error

var_dump(get_terms(get_the_ID()));

// Returns this
object(WP_Error)#6268 (2) { ["errors"]=> array(1) { ["invalid_taxonomy"]=> array(1) { [0]=> string(20) "Taxonomia inválida." } } ["error_data"]=> array(0) { } }

Why does this say its and invalid taxonomy?


Solution

  • You are using the wrong function, get_terms will return list of the terms in the taxonomy. If you want to list the terms of the post containing the custom taxonomy, you need to use the get_the_terms

    var_dump( get_the_terms(get_the_ID(), 'your-custom-taxonomy');
    

    https://developer.wordpress.org/reference/functions/get_the_terms/