WordPress at a glance

term_exists() WP 3.0.0

Check if a given taxonomy element exists. Returns either term ID or a term array if the element (term) exists.

✈ 1 time = 0.000419s = fast | 50000 times = 14.74s = slow | PHP 7.0.14, WP 4.7

No Hooks.

Return

null/int/array

  • Taxonomy element ID (term id), if the taxonomy is not specified but the term ID exists.

  • Array if taxonomy is specified and term is found:

    [ 'term_id'=> 'term id', 'term_taxonomy_id'=> 'taxonomy id' ]
  • 0/false - if the term doesn't exist.

Usage

term_exists( $term, $taxonomy, $parent );
$term(int/string) (required)
The term to check. Accepts term ID, slug, or name.
$taxonomy(string)
The taxonomy name to use
Default: ''
$parent(int)
ID of parent term under which to confine the exists search.
Default: null

Examples

#1 Usage examples

$term = term_exists( 'miscellaneous' );
// Returns term ID with 'miscellaneous' slug

$term = term_exists( 'Men Clothes' );
// Returns term ID with 'Men Clothes' name

#1.1 With a taxonomy

$term = term_exists( 'Men Clothes', 'my_tax' );
// Returns array
//    [term_id] => 80
//    [term_taxonomy_id] => 84

// Term ID
echo $term['term_id'];

// Taxonomy ID
echo $term['term_taxonomy_id'];

Notes

  • Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 3.0.0 Introduced.

Code of term exists: wp-includes/taxonomy.php WP 5.2.4

<?php
function term_exists( $term, $taxonomy = '', $parent = null ) {
	global $wpdb;

	$select     = "SELECT term_id FROM $wpdb->terms as t WHERE ";
	$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 ";

	if ( is_int( $term ) ) {
		if ( 0 == $term ) {
			return 0;
		}
		$where = 't.term_id = %d';
		if ( ! empty( $taxonomy ) ) {
			return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . ' AND tt.taxonomy = %s', $term, $taxonomy ), ARRAY_A );
		} else {
			return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) );
		}
	}

	$term = trim( wp_unslash( $term ) );
	$slug = sanitize_title( $term );

	$where             = 't.slug = %s';
	$else_where        = 't.name = %s';
	$where_fields      = array( $slug );
	$else_where_fields = array( $term );
	$orderby           = 'ORDER BY t.term_id ASC';
	$limit             = 'LIMIT 1';
	if ( ! empty( $taxonomy ) ) {
		if ( is_numeric( $parent ) ) {
			$parent              = (int) $parent;
			$where_fields[]      = $parent;
			$else_where_fields[] = $parent;
			$where              .= ' AND tt.parent = %d';
			$else_where         .= ' AND tt.parent = %d';
		}

		$where_fields[]      = $taxonomy;
		$else_where_fields[] = $taxonomy;

		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 ) ) {
			return $result;
		}

		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 );
	}

	if ( $result = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields ) ) ) {
		return $result;
	}

	return $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields ) );
}

Related Functions

From tag: Conditional tags (all)

More from category: Any taxonomy

No comments
    Hello, !     Log In . Register