функция since 2.3.0

get_objects_in_term()

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

Сигнатура

get_objects_in_term( int|int[] $term_ids, string|string[] $taxonomies, array|string $args = array() ): string[]|WP_Error

Описание

Строки $taxonomies должны существовать, прежде чем эта функция продолжит работу.
Если действительная таксономия не найдена, она вернёт WP_Error.
$terms проверяются не так, как $taxonomies, но всё же должны существовать, чтобы вернуть ID объектов.
Можно изменить порядок возврата ID объектов, используя $args с массивом ASC или DESC. Значение должно быть в ключе с именем «order».

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

The strings of $taxonomies must exist before this function will continue.
On failure of finding a valid taxonomy, it will return a WP_Error.

The $terms aren’t checked the same as $taxonomies, but still need to exist for object IDs to be returned.

It is possible to change the order that object IDs are returned by using $args with either ASC or DESC array. The value should be in the key named ‘order’.

Параметры

$term_ids int|int[] обязательный
ID термина или массив ID терминов, которые будут использованы.
$taxonomies string|string[] обязательный
Строка с именем таксономии или массив строковых значений имён таксономий.
$args array|string необязательный = array()
Изменить порядок ID объектов. order stringПорядок получения терминов. Принимает 'ASC' или 'DESC'. Значение по умолчанию 'ASC'.

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

string[]|WP_Error

WP_Error

Исходный код

wp-includes/taxonomy.php:871

function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {
	global $wpdb;

	if ( ! is_array( $term_ids ) ) {
		$term_ids = array( $term_ids );
	}
	if ( ! is_array( $taxonomies ) ) {
		$taxonomies = array( $taxonomies );
	}
	foreach ( (array) $taxonomies as $taxonomy ) {
		if ( ! taxonomy_exists( $taxonomy ) ) {
			return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
		}
	}

	$defaults = array( 'order' => 'ASC' );
	$args     = wp_parse_args( $args, $defaults );

	$order = ( 'desc' === strtolower( $args['order'] ) ) ? 'DESC' : 'ASC';

	$term_ids = array_map( 'intval', $term_ids );

	$taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'";
	$term_ids   = "'" . implode( "', '", $term_ids ) . "'";

	$sql = "SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order";

	$last_changed = wp_cache_get_last_changed( 'terms' );
	$cache_key    = 'get_objects_in_term:' . md5( $sql );
	$cache        = wp_cache_get_salted( $cache_key, 'term-queries', $last_changed );
	if ( false === $cache ) {
		$object_ids = $wpdb->get_col( $sql );
		wp_cache_set_salted( $cache_key, $object_ids, 'term-queries', $last_changed );
	} else {
		$object_ids = (array) $cache;
	}

	if ( ! $object_ids ) {
		return array();
	}
	return $object_ids;
}

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

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

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

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