I have to display a button that links to a tag, but I have to hide the button if tag doesn't exists to avoid broken links.

How can I check if a specific tag exists inside wp database?

This is what I have so far:

    $tag_path = '/tag/testing/';
     if( !$page = get_page_by_path( $tag_path ) ){ 
    //hide button link
    } else {
   //show button link

2 Answers 2


I think you're looking for term_exists function.

Example Code:

$term = term_exists('tag1', 'post_tag');
if ($term !== 0 && $term !== null) {
  echo "'tag1' post_tag exists!";
} else {
    echo "'tag1' post_tag does not exist!";

I think you can check has_tag

if(has_tag('tag_name')) {
  // do something
} else {
  // do something

Ref - https://developer.wordpress.org/reference/functions/has_tag/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.