WordPress at a glance

get_term_by() WP 1.0

Get all Term data from database by Term field and data.

Warning: $value is not escaped for 'name' $field. You must do it yourself, if required.

The default $field is 'id', therefore it is possible to also use null for field, but not recommended that you do so.

If $value does not exist, the return value will be false. If $taxonomy exists and $field and $value combinations exist, the Term will be returned.

This function will always return the first term that matches the $field- $value-$taxonomy combination specified in the parameters. If your query is likely to match more than one term (as is likely to be the case when $field is 'name', for example), consider using get_terms() instead; that way, you will get all matching terms, and can provide your own logic for deciding which one was intended.

Notes:

  • See: sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
  • Since 4.4.0 $taxonomy is optional if $field is 'term_taxonomy_id'. Converted to return a WP_Term object if $output is OBJECT.
Works based on: get_terms(), get_term()
✈ 1 time = 0.001272s = very slow | 50000 times = 4.95s = fast PHP 7.1.11, WP 4.9.6

No Hooks.

Return

WP_Term/Array/false. WP_Term instance (or array) on success. Will return false if $taxonomy does not exist or $term was not found.

Usage

get_term_by( $field, $value, $taxonomy, $output, $filter );
$field(string) (required)
Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
$value(string/int) (required)
Search for this term value
$taxonomy(string)
Taxonomy name. Optional, if $field is 'term_taxonomy_id'.
Default: ''
$output(string)
The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Term object, an associative array, or a numeric array, respectively.
Default: OBJECT
$filter(string)
Optional.
Default: raw or no WordPress defined filter will applied

Code of get_term_by: wp-includes/taxonomy.php VER 4.9.8

<?php
function get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {

	// 'term_taxonomy_id' lookups don't require taxonomy checks.
	if ( 'term_taxonomy_id' !== $field && ! taxonomy_exists( $taxonomy ) ) {
		return false;
	}

	// No need to perform a query for empty 'slug' or 'name'.
	if ( 'slug' === $field || 'name' === $field ) {
		$value = (string) $value;

		if ( 0 === strlen( $value ) ) {
			return false;
		}
	}

	if ( 'id' === $field || 'term_id' === $field ) {
		$term = get_term( (int) $value, $taxonomy, $output, $filter );
		if ( is_wp_error( $term ) || null === $term ) {
			$term = false;
		}
		return $term;
	}

	$args = array(
		'get'                    => 'all',
		'number'                 => 1,
		'taxonomy'               => $taxonomy,
		'update_term_meta_cache' => false,
		'orderby'                => 'none',
		'suppress_filter'        => true,
	);

	switch ( $field ) {
		case 'slug' :
			$args['slug'] = $value;
			break;
		case 'name' :
			$args['name'] = $value;
			break;
		case 'term_taxonomy_id' :
			$args['term_taxonomy_id'] = $value;
			unset( $args[ 'taxonomy' ] );
			break;
		default :
			return false;
	}

	$terms = get_terms( $args );
	if ( is_wp_error( $terms ) || empty( $terms ) ) {
		return false;
	}

	$term = array_shift( $terms );

	// In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the db.
	if ( 'term_taxonomy_id' === $field ) {
		$taxonomy = $term->taxonomy;
	}

	return get_term( $term, $taxonomy, $output, $filter );
}

Related Functions

From tag: Term (taxonomies terms)

More from category: Any taxonomy

No comments
    Hello, !     Log In . Register