функция since 2.7.0

is_object_in_term()

Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.

Сигнатура

is_object_in_term( int $object_id, string $taxonomy, int|string|int[]|string[] $terms = null ): bool|WP_Error

Описание

Указанные термины проверяются по term_id, названиям и слагам терминов объекта.
Термины, переданные как целые числа, проверяются только по term_id терминов объекта.
Если термины не указаны, определяет, связан ли объект с какими-либо терминами в указанной таксономии.

Оригинал (английский)

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.

Параметры

$object_id int обязательный
ID объекта (ID записи, ID ссылки, …).
$taxonomy string обязательный
Имя одной таксономии.
$terms int|string|int[]|string[] необязательный = null
ID термина, название, слаг или массив таковых для проверки.

Возвращаемое значение

bool|WP_Error

WP_Error

Исходный код

wp-includes/taxonomy.php:4902

function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
	$object_id = (int) $object_id;
	if ( ! $object_id ) {
		return new WP_Error( 'invalid_object', __( 'Invalid object ID.' ) );
	}

	$object_terms = get_object_term_cache( $object_id, $taxonomy );
	if ( false === $object_terms ) {
		$object_terms = wp_get_object_terms( $object_id, $taxonomy, array( 'update_term_meta_cache' => false ) );
		if ( is_wp_error( $object_terms ) ) {
			return $object_terms;
		}

		wp_cache_set( $object_id, wp_list_pluck( $object_terms, 'term_id' ), "{$taxonomy}_relationships" );
	}

	if ( is_wp_error( $object_terms ) ) {
		return $object_terms;
	}

	if ( empty( $object_terms ) ) {
		return false;
	}

	if ( empty( $terms ) ) {
		return true;
	}

	$terms = (array) $terms;

	$ints = array_filter( $terms, 'is_int' );
	if ( $ints ) {
		$strs = array_diff( $terms, $ints );
	} else {
		$strs =& $terms;
	}

	foreach ( $object_terms as $object_term ) {
		// If term is an int, check against term_ids only.
		if ( $ints && in_array( $object_term->term_id, $ints, true ) ) {
			return true;
		}

		if ( $strs ) {
			// Only check numeric strings against term_id, to avoid false matches due to type juggling.
			$numeric_strs = array_map( 'intval', array_filter( $strs, 'is_numeric' ) );
			if ( in_array( $object_term->term_id, $numeric_strs, true ) ) {
				return true;
			}

			if ( in_array( $object_term->name, $strs, true ) ) {
				return true;
			}
			if ( in_array( $object_term->slug, $strs, true ) ) {
				return true;
			}
		}
	}

	return false;
}

История изменений

ВерсияОписание
2.7.0 Introduced.

Что будем искать? Например,Продвижение

Этот сайт использует куки-файлы. Оставаясь на сайте, Вы соглашаетесь на их использование. Для получения дополнительной информации, пожалуйста, ознакомьтесь с политикой в отношении персональных данных.