W3cubDocs

/WordPress

get_the_tags( int $post_id )

Retrieves the tags for a post.

Parameters

$post_id

(int) (Required) Post ID.

Return

(array|false|WP_Error) Array of tag objects on success, false on failure.

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 . ' '; 
  }
}
?>

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[] $terms An array of tags for the given post.
	 */
	return apply_filters( 'get_the_tags', $terms );
}

Changelog

Version Description
2.3.0 Introduced.

© 2003–2019 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_the_tags