WordPress.org

Ready to get started?Download WordPress

Codex

Function Reference/wp get post terms

Contents

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

Return Values

(array) 
List of post tags.

Variables in Returned Object

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

Examples

//Returns All Term Items for "my_term"
$term_list = wp_get_post_terms($post->ID, 'my_term', array("fields" => "all"));
print_r($term_list);

//Returns Array of Term Names for "my_term"
$term_list = wp_get_post_terms($post->ID, 'my_term', array("fields" => "names"));
print_r($term_list);

//Returns Array of Term ID's for "my_term"
$term_list = wp_get_post_terms($post->ID, 'my_term', array("fields" => "ids"));
print_r($term_list);

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.