WordPress Get Tag Id By Tag Name

Hello Friends, This is very simple thing which I am going to share in this article. I am going to explain you how to get Tag Id by Tag name, I know you might be thinking why I need that but when I was creating one plugin then I needed to get tag names by tag id. So I made a simple function to get tag name by tag id.

So below is function to get tag name by tag id :

function get_tag_ID($tag_name) {
$tag = get_term_by('name', $tag_name, 'post_tag');
if ($tag) {
return $tag->term_id;
} else {
return 0;
}
}

So above code is used to get tag name by tag id, Just simply put this function in your theme functions.php file and then call it anywhere and pass tag name in it, it will return tag id.

I hope it will help someone in WordPress get tag id by tag name.

I am PHP FreelancerHire WordPress Developer India and Hire WordPress Programmer. If you have any projects related to WordPress or PHP you can contact me.

5 Comments

  • ket nontinget April 14, 2014 Reply

    Nice function. Thanks.

  • Brian Pater May 16, 2015 Reply

    Thanks, perfect!

    Worked like charm.

  • Eduardo Floriano July 17, 2015 Reply

    The title says “Get Tag Id By Tag Name”, and int text you say ” I am going to explain you how to get Tag name by Tag id”

  • Sher Bahadur March 21, 2017 Reply

    thank you 🙂

  • Mohammad Amin September 26, 2019 Reply

    thanks 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *