tag_exists()
Checks whether a post tag with a given name exists.
Uses: term_exists()
1 time — 0.0013721 sec (very slow) | 50000 times — 21.38 sec (slow) | PHP 7.4.25, WP 5.9.3
No Hooks.
Return
Mixed
. Returns null if the term does not exist. Returns an array of the term ID and the term taxonomy ID if the pairing exists. Returns 0 if term ID 0 is passed to the function.
Usage
tag_exists( $tag_name );
- $tag_name(int|string) (required)
- -
Examples
#1 Check the tag for existence:
// Check the existence of tags $tag = tag_exists( 'Menswear' ); if( $tag ){ print_r( $tag ); }
If the tag exists, it will print something like this:
Array ( [term_id] => 541 [term_taxonomy_id] => 541 )
Why term_id
and term_taxonomy_id
are equal, read What are Taxonomies in WordPress.
#2 term_exists and tag_exists
Function tag_exists()
is a wrapper for function term_exists() to more easily check default taxonomy terms post_tag (tags, tags). The following two variants are identical in result:
$tag_name = 'Tag #1'; // Option 1 tag_exists( $tag_name ); // Option 2 term_exists( $tag_name, 'post_tag' )
Changelog
Since 2.3.0 | Introduced. |
tag_exists() tag exists code WP 6.7.1
function tag_exists( $tag_name ) { return term_exists( $tag_name, 'post_tag' ); }