检索有效分类和术语的object_ids。

原型

get_objects_in_term( int|array $term_ids, string|array $taxonomies, array|string $args = array() )

描述

在此功能继续之前,$ taxonomy的字符串必须存在。如果找不到有效的分类法,它将返回一个WP_Error类,类似于PHP 5中的异常,除了你无法捕获它们。即便如此,你仍然可以测试WP_Error类并获取错误消息。

参数

$term_ids (int|array) (Required) 将使用的术语ID或术语ID数组。

$taxonomies (string|array) (Required) 分类名称的字符串或分类名称的字符串值数组。

$args (array|string) (Optional) 更改object_ids的顺序,ASC或DESC。

返回值

(WP_Error|array)  如果分类法不存在,那么

源文件

路径:wp-includes/taxonomy.php

<?php
...
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 ) . ":$last_changed";
	$cache = wp_cache_get( $cache_key, 'terms' );
	if ( false === $cache ) {
		$object_ids = $wpdb->get_col( $sql );
		wp_cache_set( $cache_key, $object_ids, 'terms' );
	} else {
		$object_ids = (array) $cache;
	}
	if ( ! $object_ids ){
		return array();
	}
	return $object_ids;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/get_objects_in_term/