is_object_in_term

Determine if the given object is associated with any of the given terms.

Description

(bool|WP_Error) is_object_in_term( (int) $object_id, (string) $taxonomy, (null) $terms = null ); 

The given terms are checked against the object's terms' term_ids, names and slugs. Terms given as integers will only be checked against the object's terms' term_ids. If no terms are given, determines if object is associated with any terms in the given taxonomy.

Returns (bool|WP_Error)

WP_Error on input error.

Parameters (3)

0. $object_id (int)
ID of the object (post ID, link ID, ...).
1. $taxonomy (string)
Single taxonomy name.
2. $terms — Optional. (null) => null
Term term_id, name, slug or array of said. Default null.

Usage

  1. if ( !function_exists( 'is_object_in_term' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4. // ID of the object (post ID, link ID, ...). 
  5. $object_id = -1; 
  6.  
  7. // Single taxonomy name. 
  8. $taxonomy = ''; 
  9.  
  10. // Optional. Term term_id, name, slug or array of said. Default null. 
  11. $terms = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = is_object_in_term($object_id, $taxonomy, $terms); 
  15.  

Defined (1)

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

/wp-includes/taxonomy.php  
  1. function is_object_in_term( $object_id, $taxonomy, $terms = null ) { 
  2. if ( !$object_id = (int) $object_id ) 
  3. return new WP_Error( 'invalid_object', __( 'Invalid object ID' ) ); 
  4.  
  5. $object_terms = get_object_term_cache( $object_id, $taxonomy ); 
  6. if ( false === $object_terms ) { 
  7. $object_terms = wp_get_object_terms( $object_id, $taxonomy, array( 'update_term_meta_cache' => false ) ); 
  8. if ( is_wp_error( $object_terms ) ) { 
  9. return $object_terms; 
  10.  
  11. wp_cache_set( $object_id, wp_list_pluck( $object_terms, 'term_id' ), "{$taxonomy}_relationships" ); 
  12.  
  13. if ( is_wp_error( $object_terms ) ) 
  14. return $object_terms; 
  15. if ( empty( $object_terms ) ) 
  16. return false; 
  17. if ( empty( $terms ) ) 
  18. return ( !empty( $object_terms ) ); 
  19.  
  20. $terms = (array) $terms; 
  21.  
  22. if ( $ints = array_filter( $terms, 'is_int' ) ) 
  23. $strs = array_diff( $terms, $ints ); 
  24. else 
  25. $strs =& $terms; 
  26.  
  27. foreach ( $object_terms as $object_term ) { 
  28. // If term is an int, check against term_ids only. 
  29. if ( $ints && in_array( $object_term->term_id, $ints ) ) { 
  30. return true; 
  31.  
  32. if ( $strs ) { 
  33. // Only check numeric strings against term_id, to avoid false matches due to type juggling. 
  34. $numeric_strs = array_map( 'intval', array_filter( $strs, 'is_numeric' ) ); 
  35. if ( in_array( $object_term->term_id, $numeric_strs, true ) ) { 
  36. return true; 
  37.  
  38. if ( in_array( $object_term->name, $strs ) ) return true; 
  39. if ( in_array( $object_term->slug, $strs ) ) return true; 
  40.  
  41. return false;