The get_term_link($term, $taxonomy) function takes two parameters:
- $term: This can be one of three data types: String (term name or slug), Integer (term id) or Object (term object).
- $taxonomy: This is the taxonomy name, which should already be defined with register_taxonomy() if it's not a built-in taxonomy, such as category or post_tag.
The WP_Error is thrown when either the term specified does not exist within the taxonomy, or the taxonomy has not been defined.
To have a better idea and hopefully fix your problem, it is good to see the list of taxonomies available, by using the get_terms($taxonomy). It will return an array of Taxonomy Objects, which contain the data structure of each taxonomy. For example:
Array
(
[0] => stdClass Object
(
[term_id] => 37
[name] => Beach
[slug] => beach
[term_group] => 0
[term_taxonomy_id] => 56
[taxonomy] => portfolio-category
[description] =>
[parent] => 0
[count] => 7
)
In the example I’m using a custom taxonomy I’ve created previously, called ‘portfolio-category’. Now, based on the information provided by the taxonomy object, you can get the link in 3 different ways:
- By using the taxonomy name:
get_term_link('Beach', 'portfolio-category'); - By using the taxonomy slug:
get_term_link('beach', 'portfolio-category'); - By using the taxonomy object:
get_term_link($term, 'portfolio-category');
Last, but not least… To get a comma separated list of taxonomies, you can use the following code, assuming that you have the $post_id:
$terms = get_the_term_list($post_id, 'portfolio-category', null, ', ', null);
Hope this helps!