get_objects_in_term

函式
get_objects_in_term ( $term_ids, $taxonomies, $args = array() )
引數
  • (int|int[]) $term_ids Term ID or array of term IDs of terms that will be used.
    Required:
  • (string|string[]) $taxonomies String of taxonomy name or Array of string values of taxonomy names.
    Required:
  • (array|string) $args Change the order of the object IDs, either ASC or DESC.
    Required:
    Default: array()
返回值
  • (string[]|WP_Error) An array of object IDs as numeric strings on success, WP_Error if the taxonomy does not exist.
定義位置
相關方法
is_object_in_termwp_get_object_termsget_object_term_cachewp_set_object_termsget_object_subtype
引入
2.3.0
棄用
-

get_objects_in_term函式用於檢索與指定分類法中的特定術語相關的物件(例如文章或使用者)的陣列: 這個函式可以用來檢索與特定術語相關的文章或使用者。

檢索有效分類法和術語的物件ID。

`$taxonomies`的字串必須存在,這個函式才會繼續。如果找不到有效的分類法,它將返回一個WP_Error。

`$terms`沒有像`$taxonomies`那樣被檢查,但是仍然需要存在物件ID才能被返回。

通過使用`$args`的ASC或DESC陣列,可以改變物件ID的返回順序。該值應該在名為’order’的鍵中。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
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; }
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;
}

常見問題

FAQs
檢視更多 >