term_exists

Check if Term exists.

Description

term_exists( (int|string) $term, (string) $taxonomy = '', (null) $parent = null ); 

Formerly is_term(), introduced in 2.3.0.

Parameters (3)

0. $term (int|string)
The term to check. Accepts term ID, slug, or name.
1. $taxonomy — Optional. (string) => ''
The taxonomy name to use
2. $parent — Optional. (null) => null
ID of parent term under which to confine the exists search.

Usage

  1. if ( !function_exists( 'term_exists' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4. // The term to check. Accepts term ID, slug, or name. 
  5. $term = null; 
  6.  
  7. // The taxonomy name to use 
  8. $taxonomy = ''; 
  9.  
  10. // Optional. ID of parent term under which to confine the exists search. 
  11. $parent = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = term_exists($term, $taxonomy, $parent); 
  15.  

Defined (1)

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

/wp-includes/taxonomy.php  
  1. function term_exists( $term, $taxonomy = '', $parent = null ) { 
  2. global $wpdb; 
  3.  
  4. $select = "SELECT term_id FROM $wpdb->terms as t WHERE "; 
  5. $tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE "; 
  6.  
  7. if ( is_int($term) ) { 
  8. if ( 0 == $term ) 
  9. return 0; 
  10. $where = 't.term_id = %d'; 
  11. if ( !empty($taxonomy) ) 
  12. return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A ); 
  13. else 
  14. return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) ); 
  15.  
  16. $term = trim( wp_unslash( $term ) ); 
  17. $slug = sanitize_title( $term ); 
  18.  
  19. $where = 't.slug = %s'; 
  20. $else_where = 't.name = %s'; 
  21. $where_fields = array($slug); 
  22. $else_where_fields = array($term); 
  23. $orderby = 'ORDER BY t.term_id ASC'; 
  24. $limit = 'LIMIT 1'; 
  25. if ( !empty($taxonomy) ) { 
  26. if ( is_numeric( $parent ) ) { 
  27. $parent = (int) $parent; 
  28. $where_fields[] = $parent; 
  29. $else_where_fields[] = $parent; 
  30. $where .= ' AND tt.parent = %d'; 
  31. $else_where .= ' AND tt.parent = %d'; 
  32.  
  33. $where_fields[] = $taxonomy; 
  34. $else_where_fields[] = $taxonomy; 
  35.  
  36. if ( $result = $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s $orderby $limit", $where_fields), ARRAY_A) ) 
  37. return $result; 
  38.  
  39. return $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s $orderby $limit", $else_where_fields), ARRAY_A); 
  40.  
  41. if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields) ) ) 
  42. return $result; 
  43.  
  44. return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields) );