Languages: English • get post terms 日本語 (Add your language)
Retrieve the terms for a post.
There is only a single recognized element for the $args array, called 'fields' and by default is set to 'all'. Other values can be 'names' or 'ids'. There are other defaults that can be overridden in wp_get_object_terms().
<?php $terms = wp_get_post_terms( $post_id, $taxonomy, $args ); ?>
$args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
//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); //Echo a single value - $term_list is an array of objects. You must select one of the // array entries before you can reference its properties (fields). $term_list = wp_get_post_terms($post->ID, 'my_taxonomy', array("fields" => "all")); echo $term_list[0]->description ; //Do something if a specific array value exists within a post $term_list = wp_get_post_terms($post->ID, 'product_features', array("fields" => "all")); foreach($term_list as $term_single) { echo $term_single->slug; //do something here }
Example of returned object
Array ( [0] => WP_Term Object ( [term_id] => 145 [name] => Example Category [slug] => example-cat [term_group] => 0 [term_taxonomy_id] => 145 [taxonomy] => adcpt_categories [description] => [parent] => 0 [count] => 2 [filter] => raw ) )
Since: 2.8.0
wp_get_post_terms() is located in wp-includes/post.php
.