How to retrieve all WordPress terms attached to the Post and Other Post type? Assumed I don't know/don't care about the taxonomy, term name/term id etc, the only thing I know is post_id. Thank you.

link|improve this question
feedback

2 Answers

There's a function specifically for this, it's called wp_get_post_terms().

Unfortunately, you do need to care about the taxonomy. If you don't specify a taxonomy, it will return all of the "post_tags" terms:

$terms = wp_get_post_terms( $post_id, $taxonomy, $args )
  • $post_id is the ID of the post you're working with (defaults to 0)
  • $taxonomy is the name of the taxonomy for which you want to retrieve terms (defaults to "post_tags"
  • $args is an array of overrides of other default parameters (see the Codex for details)
link|improve this answer
hello, thanks. but if I only passes $post_id, will return empty array if current post type don't have any tags taxonomy attached – takien Feb 7 at 16:54
Exactly, that's why I said you do need to care about the taxonomy. If you don't pass in a taxonomy, it gets the terms associated with the "post_tags" taxonomy. – EAMann Feb 7 at 17:17
feedback

Hmm. You could try getting all taxonomies and getting all terms associated with the post ID and taxonomy.

$taxonomies = get_taxonomies( '', 'names' );
$terms = wp_get_object_terms($post->ID, $taxonomies);

I haven't had a chance to try this out myself.

link|improve this answer
almost there, but in this case, I don't know 'names' finally i got this: $custom_post_type = get_post_type_object(get_post_type(get_the_id())); echo '<pre>'; $args = Array('object_type'=>array($custom_post_type->name)); $all_tax = get_taxonomies($args); foreach($all_tax as $tax){ $all_terms = wp_get_object_terms(get_the_id(), $tax); print_r($all_terms); } echo '</pre>'; Thanks, solved :) – takien Feb 7 at 16:55
'names' isn't a taxonomy. It is the return type. So get_taxonomies( '', 'names' ) would return an array of taxonomy names. – Ryan Meier Feb 7 at 17:14
oh ok. but if I don't pass first argument with object type, it will return all taxonomies, not only which is attached to the current post. here is the difference: echo '<pre>'; print_r(get_taxonomies('','names')); echo '</pre>'; Versus $tax_args = Array('object_type'=>array($post_type_name)); echo '<pre>'; print_r(get_taxonomies($tax_args,'names')); echo '</pre>'; thanks anyway :) – takien Feb 7 at 20:11
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.