Using WordPress ‘get_tags()’ PHP function

The get_tags() WordPress PHP function retrieves all post tags.

Usage

get_tags( $args );

Custom example:

$tags = get_tags(array('orderby' => 'count', 'order' => 'DESC'));

Parameters

  • $args (array|string): Arguments to retrieve tags. See get_terms() for additional options.

More information

See WordPress Developer Resources: get_tags()

Examples

Retrieve all tags

Retrieve all tags and display their names.

$tags = get_tags();
if ($tags) {
    foreach ($tags as $tag) {
        echo $tag->name . '<br>';
    }
}

Retrieve tags sorted by count

Retrieve tags sorted by the number of posts assigned to them in descending order.

$args = array('orderby' => 'count', 'order' => 'DESC');
$tags = get_tags($args);
if ($tags) {
    foreach ($tags as $tag) {
        echo $tag->name . '<br>';
    }
}

Retrieve tags with a specific slug

Retrieve tags with slugs ‘travel’ and ‘food’.

$args = array('slug' => array('travel', 'food'));
$tags = get_tags($args);
if ($tags) {
    foreach ($tags as $tag) {
        echo $tag->name . '<br>';
    }
}

Exclude specific tags

Retrieve all tags, excluding tags with IDs 3 and 5.

$args = array('exclude' => array(3, 5));
$tags = get_tags($args);
if ($tags) {
    foreach ($tags as $tag) {
        echo $tag->name . '<br>';
    }
}

Retrieve tags with a minimum post count

Retrieve tags that have been assigned to at least 5 posts.

$args = array('hide_empty' => true, 'number' => 5);
$tags = get_tags($args);
if ($tags) {
    foreach ($tags as $tag) {
        echo $tag->name . '<br>';
    }
}