get_term_by

函式
get_term_by ( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' )
引數
  • (string) $field Either 'slug', 'name', 'term_id' (or 'id', 'ID'), or 'term_taxonomy_id'.
    Required:
  • (string|int) $value Search for this term value.
    Required:
  • (string) $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
    Required:
    Default: (empty)
  • (string) $output Optional. 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.
    Required:
    Default: OBJECT
  • (string) $filter Optional. How to sanitize term fields. Default 'raw'.
    Required:
    Default: 'raw'
返回值
  • (WP_Term|array|false) WP_Term instance (or array) on success, depending on the `$output` value. False if `$taxonomy` does not exist or `$term` was not found.
相關
  • sanitize_term_field()
定義位置
相關方法
get_termget_user_byget_termsget_term_linkget_term_meta
引入
2.3.0
棄用
-

get_term_by: 這個函式通過一個特定的欄位值檢索一個特定的術語物件。它需要三個引數:要搜尋的欄位(名稱、lug或ID),要搜尋的值,以及該術語所屬的分類法名稱。它返回術語物件。

通過術語欄位和資料從資料庫中獲取所有術語資料。

警告:$value對於’name’$field沒有被轉義。如果需要,你必須自己做。

預設的$field是’id’,因此也可以使用null作為欄位,但不建議你這樣做。

如果$value不存在,返回值將是false。如果$taxonomy存在,並且$field和$value的組合存在,將返回術語。

這個函式將總是返回與引數中指定的`$field`-`$value`-`$taxonomy`組合匹配的第一個術語。如果你的查詢可能會匹配多個術語(例如,當`$field`是’name’時,可能會出現這種情況),考慮使用get_terms();這樣,你會得到所有匹配的術語,並且可以提供你自己的邏輯來
決定哪一個才是目的。

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 || '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 );
}

常見問題

FAQs
檢視更多 >