How to get Associated Tags to a WordPress Post? (5 Examples)

WordPress get_the_tags function is responsible for retrieving associated tags of a post. WordPress developers need to specify post ID to receive an array containing tag IDs, Names, Slugs in full-detailed term objects. Read this tutorial for general information about tagging, its usage, and testing the get_the_tags function in working examples.

5 from 5
from 1 user
(Rate this post)

What are tags, and why is tagging an essential feature for blogging systems?

Just like categories, Tags are one of the default tools to organize content in WordPress.

Administrators or developers often use tags to relate posts to each other for future listings.

For example, it is genius to use tags to display related articles to each post.

Tagging posts will help you optimize your content for search engines and improve your inbound link building.

Tags are one of the first features of WordPress editor.

As a WordPress developer who writes blog posts and Loves SEO technics, I consider tags an essential feature to use.

How to assign tags to WordPress posts?

Defining tags is simple when you are using WordPress editor. Any editor can assign tags to posts while editing them.

Add tags to WordPress post
Add tags to a WordPress post in the editor

As you can see in the screenshot I took while typing this post; you can add a new tag by typing inside the bordered area.

If a tag is available containing the typed word, WordPress will suggest it to you; If not, you can add it as a new tag by just hitting enter on your keyboard.

How to list tags assigned to a post programmatically?

Listing associated tags of a post is a smart thing to do for user experience and SEO.

WordPress introduced the get_the_tags function for developers to retrieve associated tags of a post.

get_the_tags function syntax:

get_the_tags(int|WP_Post $postId)

get_the_tags receives one required argument. Developers must specify post ID or post object for this function to run properly.

If post ID existed and tags were available, the get_the_tags function will return an array of WP_Term objects.

Examples of using get_the_tags function:

1- Retrieve Tag IDs array for a post

PHP array_map is the helper function for us to use when we want only one field of returned objects.

For this example, we want the term_id field of wp_term objects:


$postId = 1;
$tagObjects = get_the_tags($postId);
$tagIds = array();
if($tagObjects){
	$tagIds = array_map(function($tag){return $tag->term_id;}, $tagObjects);//PHP 5.3 or newer
}

2- Retrieve tag names of a post

Using PHP array_map is the solution again.


$postId = 1;
$tagObjects = get_the_tags($postId);
$tagNames = array();
if($tagObjects){
	$tagNames = array_map(function($tag){return $tag->name;},$tagObjects);
}

3- Retrieve full detailed tag objects associated to a post

get_the_tags function will return the full detailed object of post associated tags.

$postId = 1;
$tagObjects = get_the_tags($postId);

Returned data is structured like below:

Array
(
    [0] => WP_Term Object
        (
            [term_id] => 6 //a possible ID!
            [name] => lorem
            [slug] => lorem
            [term_group] => 0
            [term_taxonomy_id] => 6
            [taxonomy] => post_tag
            [description] => 
            [parent] => 0
            [count] => 2
            [filter] => raw
        )

)

4- Ordering tags associated with a post

For ordering received tags you must use the usort PHP function to set the order.

Sort returned tags by name:

$postId = 1;
$tagObjects = get_the_tags($postId);
usort($tagObjects, function($a, $b) {return strcmp($a->name, $b->name);}); //sorting by name!

Sorting returned tags by id:

$postId = 1;
$tagObjects = get_the_tags($postId);
usort($tagObjects, function($a, $b) {return $a->term_id > $b->term_id;}); //sorting by id!

5- Filtering get_the_tags function result

We must use the array_filter PHP function to filter received tags based on wp_term object available attributes.

$postId = 1;
$tagObjects = get_the_tags($postId);
//filter a tag name (i used "test" for tag name):
$filteredData = array_filter($tagObjects, function($tag) {return $tag->name === "test";});

More information about WordPress tagging and its related functions and methods

WordPress’s official website is the most comprehensive place to find information about WordPress core features.

I recommend you search the official WordPress website to find all available functions related to tags.

Read my tutorial “How to Query WordPress Categories, Tags and Taxonomies“, to dive deeper into the WordPress taxonomy feature.

If you are new to WordPress development, learn how to create a plugin in this tutorial:

How to create a Hello World plugin for WordPress

What do you think about "How to get Associated Tags to a WordPress Post? (5 Examples)"?

Your rating submit was successfull.

Your rating submit was not successfull.

Menu