Title: wp_get_post_terms
Published: April 25, 2014
Last modified: August 20, 2026

---

# wp_get_post_terms( int $post_id, string|string[] $taxonomy = 'post_tag', array $args = array() ): array|󠀁[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)󠁿

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/wp_get_post_terms/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_get_post_terms/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_get_post_terms/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_get_post_terms/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_get_post_terms/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_get_post_terms/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_get_post_terms/?output_format=md#wp--skip-link--target)

Retrieves the terms for a post.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_get_post_terms/?output_format=md#parameters)󠁿

 `$post_id`intoptional

The Post ID. Does not default to the ID of the global $post. Default 0.

`$taxonomy`string|string[]optional

The taxonomy slug or array of slugs for which to retrieve terms. Default `'post_tag'`.

Default:`'post_tag'`

`$args`arrayoptional

Term query parameters. See [WP_Term_Query::__construct()](https://developer.wordpress.org/reference/classes/wp_term_query/__construct/)
for supported arguments.

 * `fields` string
 * Term fields to retrieve. Default `'all'`.

Default:`array()`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_get_post_terms/?output_format=md#return)󠁿

 array|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/) Array
of [WP_Term](https://developer.wordpress.org/reference/classes/wp_term/) objects
on success or empty array if no terms were found.
 [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
object if `$taxonomy` doesn’t exist.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_get_post_terms/?output_format=md#source)󠁿

    ```php
    function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array() ) {
    	$post_id = (int) $post_id;

    	$defaults = array( 'fields' => 'all' );
    	$args     = wp_parse_args( $args, $defaults );

    	$tags = wp_get_object_terms( $post_id, $taxonomy, $args );

    	return $tags;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/post.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/post.php#L4452)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/post.php#L4452-L4461)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_get_post_terms/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_get_object_terms()](https://developer.wordpress.org/reference/functions/wp_get_object_terms/)`wp-includes/taxonomy.php` |

Retrieves the terms associated with the given object(s), in the supplied taxonomies.

  | 
| [wp_parse_args()](https://developer.wordpress.org/reference/functions/wp_parse_args/)`wp-includes/functions.php` |

Merges user defined arguments into defaults array.

  |

| Used by | Description | 
| [WP_REST_Menu_Items_Controller::get_menu_id()](https://developer.wordpress.org/reference/classes/wp_rest_menu_items_controller/get_menu_id/)`wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php` |

Gets the id of the menu that the given menu item belongs to.

  | 
| [WP_Customize_Nav_Menu_Item_Setting::populate_value()](https://developer.wordpress.org/reference/classes/wp_customize_nav_menu_item_setting/populate_value/)`wp-includes/customize/class-wp-customize-nav-menu-item-setting.php` |

Ensure that the value is fully populated with the necessary properties.

  | 
| [wp_get_post_tags()](https://developer.wordpress.org/reference/functions/wp_get_post_tags/)`wp-includes/post.php` |

Retrieves the tags for a post.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_get_post_terms/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.8.0](https://developer.wordpress.org/reference/since/2.8.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_get_post_terms/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 6 content](https://developer.wordpress.org/reference/functions/wp_get_post_terms/?output_format=md#comment-content-2422)
 2.    [Dustin Falgout](https://profiles.wordpress.org/lots0logs/)  [  9 years ago  ](https://developer.wordpress.org/reference/functions/wp_get_post_terms/#comment-2422)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_post_terms%2F%23comment-2422)
     Vote results for this note: 16[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_post_terms%2F%23comment-2422)
 4.  The difference between this function and `get_the_terms()` is that this function’s
     results are not cached (thus, it’ll hit the database every time its called).
 5.   * This is not entirely true. Another key distinction is that `get_the_terms()`
        does not allow the third parameter: `$args`.
      * [Joshua Fredrickson](https://profiles.wordpress.org/joshf/) [6 years ago](https://developer.wordpress.org/reference/functions/wp_get_post_terms/#comment-4238)
 6.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_post_terms%2F%3Freplytocom%3D2422%23feedback-editor-2422)
 7.   [Skip to note 7 content](https://developer.wordpress.org/reference/functions/wp_get_post_terms/?output_format=md#comment-content-421)
 8.    [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/wp_get_post_terms/#comment-421)
 9.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_post_terms%2F%23comment-421)
     Vote results for this note: 4[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_post_terms%2F%23comment-421)
 10. **Examples**
 11.     ```php
         //Returns All Term Items for "my_taxonomy".
         $term_list = wp_get_post_terms( $post->ID, 'my_taxonomy', array( 'fields' => 'all' ) );
         print_r( $term_list );
     
         // Returns Array of Term Names for "my_taxonomy".
         $term_list = wp_get_post_terms( $post->ID, 'my_taxonomy', array( 'fields' => 'names' ) );
         print_r( $term_list );
     
         // Returns Array of Term ID's for "my_taxonomy".
         $term_list = wp_get_post_terms( $post->ID, 'my_taxonomy', array( 'fields' => 'ids' ) );
         print_r( $term_list );
         ```
     
 12.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_post_terms%2F%3Freplytocom%3D421%23feedback-editor-421)
 13.  [Skip to note 8 content](https://developer.wordpress.org/reference/functions/wp_get_post_terms/?output_format=md#comment-content-4579)
 14.   [tradesouthwest](https://profiles.wordpress.org/tradesouthwestgmailcom/)  [  6 years ago  ](https://developer.wordpress.org/reference/functions/wp_get_post_terms/#comment-4579)
 15. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_post_terms%2F%23comment-4579)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_post_terms%2F%23comment-4579)
 16. **Get term category to only run function if the post (CPT) has that category:**
 17.     ```php
         /**
          * Get category and add text to that page, only.
          * 
          * @param  string $terms Get post terms
          * @return string        Text
          */
         function wpdocs_additional_measurements() {
              global $post;
     
             $innertext = "Hemmed tarps have seatbelt material woven into the hems.";
             $terms = wp_get_post_terms( $post->ID, 'product_cat' );
     
             foreach ( $terms as $term ) {
                 $categories[] = $term->slug;
             }
     
             // Look for term/category by slug	
             if ( in_array( 'custom-tarps', $categories ) ) {
                 printf( '<p>%s</p>',
                         esc_html__( $innertext, 'woocommerce' )
                 );
            }
     
         } 
         add_action( 'woocommerce_before_add_to_cart_button', 'wpdocs_additional_measurements', 9 ); 
         ```
     
 18. Use curly brackets recommended but not needed on one expression of foreach
 19.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_post_terms%2F%3Freplytocom%3D4579%23feedback-editor-4579)
 20.  [Skip to note 9 content](https://developer.wordpress.org/reference/functions/wp_get_post_terms/?output_format=md#comment-content-5353)
 21.   [Mehedi Foysal](https://profiles.wordpress.org/mehedi890/)  [  5 years ago  ](https://developer.wordpress.org/reference/functions/wp_get_post_terms/#comment-5353)
 22. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_post_terms%2F%23comment-5353)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_post_terms%2F%23comment-5353)
 23. **Get all taxonomy slug of a post**
 24.     ```php
         function post_taxonomy_slug_array( $tax_name = 'category' ) {
     
             $terms = wp_get_post_terms( get_the_ID(), $tax_name, array( "fields" => "slugs" ) );
             if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
     
                 $term_array = array();
     
                 foreach ( $terms as $term ) {
                     $term_array[] = $term;
                 }
     
                 return $term_array;
             }
     
             return '';
         }
         ```
     
 25.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_post_terms%2F%3Freplytocom%3D5353%23feedback-editor-5353)
 26.  [Skip to note 10 content](https://developer.wordpress.org/reference/functions/wp_get_post_terms/?output_format=md#comment-content-5618)
 27.   [awaismushtaq](https://profiles.wordpress.org/awaismushtaq/)  [  5 years ago  ](https://developer.wordpress.org/reference/functions/wp_get_post_terms/#comment-5618)
 28. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_post_terms%2F%23comment-5618)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_post_terms%2F%23comment-5618)
 29. You can get the taxonomy terms HTML using this function am_get_post_term
 30.     ```php
         function am_get_post_terms( $post_id = 0, $taxonomy = 'category', $custom_html = ['ul','li','a'] ) 
         {
         	$post_terms = wp_get_post_terms( $post_id, $taxonomy );
     
         	$custom_html[0] = isset($custom_html[0]) ? $custom_html[0] : 'ul';
         	$custom_html[1] = isset($custom_html[1]) ? $custom_html[1] : 'li';
         	$custom_html[2] = isset($custom_html[2]) ? $custom_html[2] : 'a'; 
     
         	$post_term_html = '';
         	$post_term_html .= '<' . $custom_html[0] . '>';
         	foreach( $post_terms as $p_term ) {
         		$post_term_html .= '<' . $custom_html[1] . '><' . $custom_html[2] . ' href="'. get_term_link( $p_term->term_id, $taxonomy ) .'">'. $p_term->name .'</' . $custom_html[2]  . '></' . $custom_html[1] . '>';
         	}
         	$post_term_html .= '</' . $custom_html[0] . '>';
     
         	return $post_term_html;
     
         }
     
         Call the function in single.php file work for all CPT:
     
         [php]echo am_get_post_terms( get_the_ID(), 'category',['div','span','a'] );
         ```
     
 31. [/php]
 32.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_post_terms%2F%3Freplytocom%3D5618%23feedback-editor-5618)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_post_terms%2F)
before being able to contribute a note or feedback.