WordPress at a glance

get_tags() WP 2.3.0

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

Works based on: get_terms()
Hooks from the function
Return

Array/int. 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

#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;

Notes

  • See: get_terms() For list of arguments to pass.

Changelog

Since 2.3.0 Introduced.

Code of get tags: wp-includes/category.php WP 5.2.4

<?php
function get_tags( $args = '' ) {
	$tags = get_terms( 'post_tag', $args );

	if ( empty( $tags ) ) {
		$return = array();
		return $return;
	}

	/**
	 * Filters the array of term objects returned for the 'post_tag' taxonomy.
	 *
	 * @since 2.3.0
	 *
	 * @param WP_Term[]|int $tags Array of 'post_tag' term objects, or a count thereof.
	 * @param array         $args An array of arguments. @see get_terms()
	 */
	$tags = apply_filters( 'get_tags', $tags, $args );
	return $tags;
}

Related Functions

From category: Tags

No comments
    Hello, !     Log In . Register