WordPress.org

Codex

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 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().

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) 
An array of taxonomy terms, or empty array if no terms are found. 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 ;

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

Returns

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
        )

)

Notes

Change Log

Since: 2.8.0

Source File

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

Related

wp_remove_object_terms()

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