get_the_tags() WordPress Function

The get_the_tags() function in WordPress allows you to retrieve the tags assigned to a post. This is useful if you want to display a list of tags for a post on your website.

get_the_tags( int|WP_Post $post_id ) #

Retrieves the tags for a post.


Parameters

$post_id

(int|WP_Post)(Required)Post ID or object.


Top ↑

Return

(WP_Term[]|false|WP_Error) Array of WP_Term objects on success, false if there are no terms or the post does not exist, WP_Error on failure.


Top ↑

More Information

This function returns an array of objects, one object for each tag assigned to the post. If this function is used in The Loop, then no ID need be passed.

This function does not display anything; you should access the objects and then echo or otherwise use the desired member variables.

The following example displays the tag name of each tag assigned to the post (this is like using the_tags(), but without linking each tag to the tag view, and using spaces instead of commas):

<?php
$posttags = get_the_tags();
if ($posttags) {
  foreach($posttags as $tag) {
    echo $tag->name . ' '; 
  }
}
?>

Top ↑

Source

File: wp-includes/category-template.php

function get_the_tags( $post_id = 0 ) {
	$terms = get_the_terms( $post_id, 'post_tag' );

	/**
	 * Filters the array of tags for the given post.
	 *
	 * @since 2.3.0
	 *
	 * @see get_the_terms()
	 *
	 * @param WP_Term[]|false|WP_Error $terms Array of WP_Term objects on success, false if there are no terms
	 *                                        or the post does not exist, WP_Error on failure.
	 */
	return apply_filters( 'get_the_tags', $terms );
}


Top ↑

Changelog

Changelog
VersionDescription
2.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.