WordPress.org

Ready to get started?Download WordPress

Codex

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

Function Reference/wp get post tags

Contents

Description

Retrieve the list of tags for a post.

Usage

<?php wp_get_post_tags$post_id$args ?>

Parameters

$post_id
(integer) (optional) The Post ID.
Default: 0
$args
(array) (optional) Overwrite the defaults. See wp_get_object_terms() for a list of valid options that can be passed.
Default: array

Return Values

(array) 
List of post tags.

Examples

For a post with tags tag2, tag5 and tag6, the code

$t = wp_get_post_tags($post->ID);
print_r($t);

Displays

Array
(
   [0] => stdClass Object
       (
           [term_id] => 4
           [name] => tag2
           [slug] => tag2
           [term_group] => 0
           [term_taxonomy_id] => 4
           [taxonomy] => post_tag
           [description] => 
           [parent] => 0
           [count] => 7
       )

   [1] => stdClass Object
       (
           [term_id] => 7
           [name] => tag5
           [slug] => tag5
           [term_group] => 0
           [term_taxonomy_id] => 7
           [taxonomy] => post_tag
           [description] => 
           [parent] => 0
           [count] => 6
       )

   [2] => stdClass Object
       (
           [term_id] => 16
           [name] => tag6
           [slug] => tag6
           [term_group] => 0
           [term_taxonomy_id] => 16
           [taxonomy] => post_tag
           [description] => 
           [parent] => 0
           [count] => 2
       )

)


To get a list of only the tag IDs for a particular post:

global $post;
$tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );

and assuming the same dataset as the first example, $tag_ids would contain

[4, 7, 16]

Notes

Change Log

Since: 2.3.0

Source File

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

Related

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