How to Check if Post has a Specific Tag in WordPress

2.7777777777778 from 5
from 9 user
(Rate this post)
How to Check if Post has a Specific Tag in WordPress

To check if a post is in a tag or any of multiple tags, WP developers use the has_tag function. This function accepts two parameters to specify the tag and the post. You can execute functions when developing WordPress functionalities based on post tags using the returned boolean value by the WP has_tag function. My review for the WP has_tag function contains a practical example!

The has_tag function: Checks if a Post has a Specific Tag or Tags

Syntax of the has_tag function:

has_tag(string|int|array $tag='', int|object $post=null)

As it is obvious from the syntax, the has_tags() function has the ability to determine if a specific tag or specified multiple tags are added to a post by the author.

Two optional parameters, $tag & $post, are what this function asks you to return an exact boolean answer for your request.

The $tag can be String if you want to check the tag’s existence in a post with its slug or name.

Use Integer value for $tag if you have the ID of a tag and you want to check if it exists in a WP post.

$tag also can be an array containing multiple tag IDs, Slugs, or names. In case of encountering with array value for the $tag parameter, the has_tag() function will return a true value if any of the given tags are set for the specified post.

If the $tag parameter is not set for the has_tag() function, it will check if the post has any tags at all.

The $post parameter is also optional; this means developers can skip determining the post if they are targeting the current queried post.

For example, If you execute the has_tag() function in single.php file of the active theme, WP will check if the current post has any tags at all, and it will be true if the post has even one tag.

Learn how to detect tags in a WP post with examples

Either If you are looking for a way to check if a single tag exists in a WordPress post or if any of a collection of tags exists there, you can use the has_tag() function.

Check if a post exists in a specific tag

If you can provide a string value for tag slug or tag name or an integer value for tag ID, you just need to pass it to has_tag function.

In this example, I checked the existence of a tag with tutorial-articles slug in a post with ID 1.

//set post id (set NULL for the current queried post)
$postId = 1;

// use tag slug, tag name or term ID (int) to check existance
$tag = "tutorial-articles";

// check if tutorial-articles tag is set for the post
if(has_tag($tag, $postId)){
	//post exists, do stuff:
	echo "This post exists in the tag.";
}
else{
	echo "This post does NOT exist in the tag.";
}

To check the existence of the tag in the current queried post, you can just skip declaring $postId, or make it Null.

Check if a post exists in any of multiple tags

If you have multiple tags to check the post’s existence in them, you can still use the has_tag function.
You just need to declare the $tag parameter as an Array of tag slugs, names, or tag IDs.

In the below example, I’ll check if the post with ID 1 exists in any of the given tag slugs.

//set post id (set NULL for the current queried post)
$postId = 1;

// check exitence of post in one of these array values
$tags = array("tutorial-articles", "top-tutorials", "recommended-articles");

// check if good-post tag is set for the post
if(has_tag($tags, $postId)){
	//post exists, do stuff:
	echo "This post does NOT exist in one of the given tags.";
}
else{
	echo "This post does exist in any of these tags.";
}

The $tags variable that I declared can be an array of tag slugs, names (string), or IDs (integer).

What do you think about "How to Check if Post has a Specific Tag in WordPress"?

Your rating submit was successfull.

Your rating submit was not successfull.

Menu