WordPress.org

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp get post terms

Description

Retrieve the terms for a post.

There is only one default for this function, 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().

Usage

<?php $terms wp_get_post_terms$post_id$taxonomy$args ); ?>

Parameters

$post_id
(integer) (optional) The Post ID
Default: 0
$taxonomy
(string|array) (optional) The taxonomy for which to retrieve terms. Defaults to post_tag.
Default: 'post_tag'
$args
(array) (optional) Overwrite the defaults
Default: array

Default Arguments

$args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');

Return Values

(array|WP_Error) 
List of post tags. WP_Error if $taxonomy does not exist. See is_wp_error() for more information.

Variables in Returned Object

term_id (int) 
the id of the term itself
name (string) 
the term name
slug (string) 
a slug generated from the term name
term_group (int) 
the term_id of the parent term (also stored as 'parent')
term_taxonomy_id (int) 
the id of the taxonomy that the term belongs to
taxonomy (string) 
the name of the taxonomy that the term belongs to
description (string) 
the taxonomy description
parent (int) 
the term_id of the parent term (also stored as 'term_group')
count (int) 
the number of uses of this term

Examples

//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 ;

Notes

Change Log

Since: 2.8.0

Source File

wp_get_post_terms() is located in wp-includes/post.php.

Related

See also index of Function Reference and index of Template Tags.