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.


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)
  • 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 '12 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 '12 at 17:17

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.

  • 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 '12 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 '12 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 '12 at 20:11

To get all terms for all taxonomies attached to a post, you can use this function:

function my_post_terms() {

    // Get an array of all taxonomies for this post
    $taxonomies = get_taxonomies( '', 'names' );

    // Are there any taxonomies to get terms from?
    if ( $taxonomies ) {    

        // Call the wp_get_post_terms function to retrieve all terms. It accepts an array of taxonomies as argument. 
        $arr_terms = wp_get_post_terms( get_the_ID(), array_values( $taxonomies ) , array( "fields" => "names" ) );

        // Convert the terms array to a string
        $terms = implode( ' ',$arr_terms );

        // Get out of here
        return $terms;

Now you can use it in your templates:

<?php echo my_post_terms(); ?>

If you need an HTML list of all terms or links, simply loop over the $arr_terms array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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