has_tag()
Checks if the current post has any of the given tags. A conditional tag.
The given in $tag
parameter tags are checked against the post's tags — against: term_ids, names, or slugs. If only integers are passed it will be checked against the post's tags IDs (term_ids).
If no tags are given, determines if the post has any tag.
Uses: has_term()
1 time — 0.001853 sec (very slow) | 50000 times — 96.58 sec (very slow)
No Hooks.
Return
true|false
. True if the current post has any of the given tags (or any tag, if no tag specified). False otherwise.
Usage
has_tag( $tag, $post );
- $tag(string/int/array)
- The tag name/term_id/slug or array of them to check for.
Default: '' - $post(int/object)
- Post to check instead of the current post.
Default: null
Examples
#1 Check if the current post has any tags
<?php if( has_tag() ) echo 'The current post has tags!'; ?>
#2 Check if the post has kino tag
<?php if( has_tag( 'kino' ) ) echo 'The current post has tag `kino`'; ?>
#3 Check if the post 56 has tags 49 and 89 (you can use their names or slugs instead of the ids)
<?php if( has_tag( [45,89], 56 ) ) echo 'Post 56 (ID) has tags ID 45 and 89'; ?>
Changelog
Since 2.6.0 | Introduced. |
Since 2.7.0 | Tags given as integers are only checked against the post's tags' term_ids, not names or slugs. |
Since 2.7.0 | Can be used outside of the WordPress Loop if $post is provided. |
has_tag() has tag code WP 6.6.1
function has_tag( $tag = '', $post = null ) { return has_term( $tag, 'post_tag', $post ); }