get_tag

Retrieve post tag by tag ID or tag object.

Description

(WP_Term|array|WP_Error|null) get_tag( (int|WP_Term|object) $tag, (constant) $output = OBJECT, (string) $filter = 'raw' ); 

If you pass the $tag parameter an object, which is assumed to be the tag row object retrieved the database. It will cache the tag data.

If you pass $tag an integer of the tag ID, then that tag will be retrieved from the database, if it isn't already cached, and pass it back.

If you look at get_term(), then both types will be passed through several filters and finally sanitized based on the $filter parameter value.

Returns (WP_Term|array|WP_Error|null)

Tag data in type defined by $output parameter. WP_Error if $tag is empty, null if it does not exist.

Parameters (3)

0. $tag (int|WP_Term|object)
A tag ID or object.
1. $output — Optional. (constant) => OBJECT
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.
2. $filter — Optional. (string) => 'raw'
Default is raw or no WordPress defined filter will applied.

Usage

  1. if ( !function_exists( 'get_tag' ) ) { 
  2. require_once ABSPATH . WPINC . '/category.php'; 
  3.  
  4. // A tag ID or object. 
  5. $tag = null; 
  6.  
  7. // Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to 
  8. // a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT. 
  9. $output = OBJECT; 
  10.  
  11. // Optional. Default is raw or no WordPress defined filter will applied. 
  12. $filter = 'raw'; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = get_tag($tag, $output, $filter); 
  16.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/category.php  
  1. function get_tag( $tag, $output = OBJECT, $filter = 'raw' ) { 
  2. return get_term( $tag, 'post_tag', $output, $filter );