Using WordPress ‘get_tag()’ PHP function

The get_tag() WordPress PHP function retrieves a post tag by tag ID or tag object.


get_tag( $tag, $output, $filter )

Custom example:

$tag_id = 42;
$tag_data = get_tag($tag_id, ARRAY_A, 'display');


  • $tag (int|WP_Term|object) (Required) – A tag ID or object.
  • $output (string) (Optional) – The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Term object, an associative array, or a numeric array, respectively. Default: OBJECT.
  • $filter (string) (Optional) – How to sanitize tag fields. Default ‘raw’. Default: ‘raw’.

More information

See WordPress Developer Resources: get_tag()


Retrieve tag by ID

Retrieve a tag using its ID and display the tag name.

$tag_id = 42;
$tag = get_tag($tag_id);
echo 'Tag name: ' . $tag->name;

Retrieve tag as an associative array

Retrieve a tag as an associative array and display the tag slug.

$tag_id = 42;
$tag = get_tag($tag_id, ARRAY_A);
echo 'Tag slug: ' . $tag['slug'];

Retrieve tag with sanitized fields

Retrieve a tag with sanitized fields for display and output the tag description.

$tag_id = 42;
$tag = get_tag($tag_id, OBJECT, 'display');
echo 'Tag description: ' . $tag->description;

Retrieve tag by WP_Term object

Retrieve a tag using a WP_Term object and output the tag count.

$tag_object = new WP_Term(42);
$tag = get_tag($tag_object);
echo 'Tag count: ' . $tag->count;

Retrieve tag as a numeric array

Retrieve a tag as a numeric array and display the tag taxonomy.

$tag_id = 42;
$tag = get_tag($tag_id, ARRAY_N);
echo 'Tag taxonomy: ' . $tag[2];