has_term( string|int|array $term = '', string $taxonomy = '', int|WP_Post $post = null ): bool
- Since
- 3.1.0
- Source
wp-includes/category-template.php:1542
Checks if the current post has any of given terms.
Description
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.
Parameters
$termstring|int|arrayoptional- The term name/term_id/slug, or an array of them to check for. Default empty.Default:
'' $taxonomystringoptional- Taxonomy name. Default empty.Default:
'' $postint|WP_Postoptional- Post to check. Defaults to the current post.Default:
null
Return
bool- True if the current post has any of the given terms (or any term, if no term specified). False otherwise.
Uses · 3
- get_post()Retrieves post data given a post ID or post object.
- is_object_in_term()Determines if the given object is associated with any of the given terms.
- is_wp_error()Checks whether the given variable is a WordPress Error.
Used by · 4
- has_category()Checks if the current post has any of given category.
- has_post_format()Check if a post has any of the given formats, or any format.
- has_tag()Checks if the current post has any of given tags.
- redirect_canonical()Redirects incoming links to the proper URL based on the site url.
Source
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;}History
Introduced in 3.1.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
7.0.4
Parameter
$post retyped from int|WP_Post to int|WP_Post|null.verified against source3.1.0
Introduced.from the docblock
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/category-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.