has_term

Check if the current post has any of given terms.

Description

has_term( (string) $term = '', (string) $taxonomy = '', (null) $post = null ); 

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 (3)

0. $term — Optional. (string) => ''
The term name/term_id/slug or array of them to check for.
1. $taxonomy — Optional. (string) => ''
The taxonomy.
2. $post — Optional. (null) => null
Post to check instead of the current post.

Usage

  1. if ( !function_exists( 'has_term' ) ) { 
  2. require_once ABSPATH . WPINC . '/category-template.php'; 
  3.  
  4. // Optional. The term name/term_id/slug or array of them to check for. 
  5. $term = ''; 
  6.  
  7. // The taxonomy. 
  8. $taxonomy = ''; 
  9.  
  10. // Optional. Post to check instead of the current post. 
  11. $post = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = has_term($term, $taxonomy, $post); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/category-template.php  
  1. function has_term( $term = '', $taxonomy = '', $post = null ) { 
  2. $post = get_post($post); 
  3.  
  4. if ( !$post ) 
  5. return false; 
  6.  
  7. $r = is_object_in_term( $post->ID, $taxonomy, $term ); 
  8. if ( is_wp_error( $r ) ) 
  9. return false; 
  10.  
  11. return $r;