get_tags()WP 2.3.0

Retrieve an array of tags objects. Tags can be retrieved by different criteria (see $args parameter).

Hooks from the function

Return

WP_Term[]|Int|WP_Error. An array of WP_Term objects, or a count thereof.

Each object has these properties:

  • term_id - ID of the tag;
  • name - name of the tag;
  • slug - slug;
  • term_group - group (not used);
  • term_taxonomy_id - ID of the taxonomy item (usually the same as term_id);
  • taxonomy - the name of the taxonomy;
  • description - description;
  • parent - ID of the parent's taxonomy (not used for tags);
  • count - posts count.

Usage template

$terms = get_tags( [
	'number'       => 0,
	'offset'       => 0,
	'orderby'      => 'id',
	'order'        => 'ASC',
	'hide_empty'   => true,
	'fields'       => 'all',
	'slug'         => '',
	'hierarchical' => true,
	'name__like'   => '',
	'pad_counts'   => false,
	'get'          => '',
	'child_of'     => 0,
	'parent'       => '',
] );

Usage

get_tags( $args );
$args(string/array)

Tag arguments to use when retrieving tags.

See get_terms() for the list of arguments.

Default: ''

Examples

0

#1 Display the list of tags, in the form of links to each tag.

And set for the <a> tag a unique class and attribute title:

$tags = get_tags();
$html = '<div class="post_tags">';

foreach ( $tags as $tag ) {
	$tag_link = get_tag_link( $tag->term_id );

	$html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
	$html .= "{$tag->name}</a>";
}

$html .= '</div>';

echo $html;

Changelog

Since 2.3.0 Introduced.

get_tags() code WP 6.8

function get_tags( $args = '' ) {
	$defaults = array( 'taxonomy' => 'post_tag' );
	$args     = wp_parse_args( $args, $defaults );

	$tags = get_terms( $args );

	if ( empty( $tags ) ) {
		$tags = array();
	} else {
		/**
		 * Filters the array of term objects returned for the 'post_tag' taxonomy.
		 *
		 * @since 2.3.0
		 *
		 * @param WP_Term[]|int|WP_Error $tags Array of 'post_tag' term objects, a count thereof,
		 *                                     or WP_Error if any of the taxonomies do not exist.
		 * @param array                  $args An array of arguments. See {@see get_terms()}.
		 */
		$tags = apply_filters( 'get_tags', $tags, $args );
	}

	return $tags;
}