Tags in WordPress – Tips and Tricks

Code Tips & Tricks Web Development WordPress

What are Tags? How is it different from Category?

Tag, in general, is used for categorizing the referred item into something specific. In WordPress, a tag is a predefined taxonomy, just like category. However, a tag is not the same as a category.

We use category to segregate our post items into a broader channel. We use a tag for segregating the items into a narrower channel or type. It can be referred to as a keyword for the topic, on which the post is based. For example, if you start a blog writing about technology, you can categorize your post as mobile app development, web development, business skills, computer skills. When you write about Android Application development and explain RecyclerView in your post, you can tag it with android, recyclerview, listview.

Tags in WordPress, for a post, is completely optional. When you do not add a category to a post, WP automatically assigns a default category to the post. However, it is not the case with tags. If you do not assign a tag to a post, there is no default tag that is automatically added. On the other hand, you can add as many numbers of tags you want to a post.

There is an archive page for tag taxonomy, which displays the list of post tagged with that particular tag. Tagging makes the content segregation easy and convenient for the site administrators, for the content creators as well as for the readers.

You should anyway try to limit the number of tags you associate with a post, keeping it specific and precise. This will prevent the database from overpopulating with unnecessary information as well as show the relevance of your tag with the post.

In this article, you will get a few tips and tricks you might need while customizing tags for your WordPress website.

Check if a WP post has tags

Inside the loop:

if ( has_tag() ) {
    // Do something
} else {
    // Do something else
}

Outside the loop, or for a specific post, you need to pass the post id.

if (get_the_tags( $your_post_id ) ) { 
    // Do something 
} else { 
    // Do something else }

More on functions: has_tag(), get_the_tags()

Display the tag links for WP posts

To get the list of tags, with the specific link, use the following code in your template, within the loop:

<?php 
    // the_tags( $before, $separator, $after )
    the_tags( '', ',', '' );
?>

In the above snippet, if my post is tagged with “Android, RecyclerView, CardView“, the output will be the list of tag, separated by a comma, with the link to archive pages of the tags(there is however no link here).
Android, RecyclerView, CardView

Check if a WP post has a specific tag

Inside the loop:

// Defaults to current post
if ( has_tag( 'your-specific-tag' ) ) {
    // Do something
} else {
    // Do something else
}

Outside the loop, or for a specific post, pass it as the second parameter to has_tag()

if ( has_tag( 'your-specific-tag', $your_post ) ) {
    // Do something
} else {
    // Do something else
}

Count the number of posts associated with a Tag

You can search by the tag id, tag name or the tag slug.

// $term_data = get_term_by( $field, $value, $taxonomy)

$taxonomy = "post_tag"; // can be category, post_tag, or custom taxonomy name
 
// By tag id
$tag_id = 1;
$term_data = get_term_by('id', $tag_id, $taxonomy);
 
// By tag name
$tag_name = 'RecyclerView';
$term_data = get_term_by('name', $tag_name, $taxonomy);
 
// By tag slug
$tag_slug = 'recyclerview';
$term_data = get_term_by('slug', $tag_slug, $taxonomy);
 
// Fetch the count
$post_count = $term_data->count;

More on the function: get_term_by()

Count the number of tags in a post

Inside the loop:

// No of tags for the post
$tag_count = count(get_tags());

or

// No of tags for the post
$tag_count = count(wp_get_post_terms($post->ID));

More on the functions: get_tags(), wp_get_post_terms()

Tag archive query for CPT

The default archives for category and tag or any custom taxonomy is fetched only for post post type. For enabling the tag archive for your custom post type, you need to add the following to your functions.php:

function archive_cpt_tags( $query ) {
    if ( $query->is_tag() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post', 'your_cpt1', 'your_cpt2' ) );
    }
}
add_action( 'pre_get_posts', 'archive_cpt_tags' );

 

Conclusion

Tags are an important part of any website. You just need to make proper and clean use of it. Consider from the user point of view while assigning categories or tags in wordpress to your posts. That’s all!

Let us know more tips that we can add up in the post to make it better.