WordPress at a glance

has_term() WP 1.0

Check if the current post has any of given terms.

The given terms are checked against the post's terms' term_ids, names and slugs. Terms given as integers will only be checked against the post's terms' term_ids. If no terms are given, determines if post has any terms.

Works based on: is_object_in_term()
✈ 1 time = 0.003337s = very slow | 50000 times = 0.82s = very fast | PHP 7.1.5, WP 4.8.1

No Hooks.

Return

true/false. True if the current post has any of the given tags (or any tag, if no tag specified).

Usage

has_term( $term, $taxonomy, $post );
$term(string/int/array)
The term name/term_id/slug or array of them to check for.
Default: ''
$taxonomy(string)
Taxonomy name
Default: ''
$post(int/object)
Post to check instead of the current post.
Default: null

Code of has_term: wp-includes/category-template.php VER 5.0.1

<?php
function has_term( $term = '', $taxonomy = '', $post = null ) {
	$post = get_post($post);

	if ( !$post )
		return false;

	$r = is_object_in_term( $post->ID, $taxonomy, $term );
	if ( is_wp_error( $r ) )
		return false;

	return $r;
}

Related Functions

From tag: Term (taxonomies terms)

More from tag: Conditional tags (all)

More from category: For posts

No comments
    Hello, !     Log In . Register