get_term_by

Sponsored Link

タームフィールドとデータで、データベースからすべてのタームデータを取得します。

Sponsored Link

Get all Term data from database by Term field and data.

警告:$valueは ‘name’のfieldにエスケープされていません。必要であれば、あなた自身が行う必要があります。

デフォルトの$fieldは’id’です。そのため、フィールドにnullを使用することも可能ですが、あなたがそうすることはお勧めしません。

$valueが存在しない場合、戻り値はfalseになります。$taxonomyが存在し、$fieldと$valueの組み合わせが存在する場合は、タームが返されます。

Warning: $value is not escaped for ‘name’ $field. You must do it yourself, if required.

The default $field is ‘id’, therefore it is possible to also use null for field, but not recommended that you do so.

If $value does not exist, the return value will be false. If $taxonomy exists and $field and $value combinations exist, the Term will be returned.

get_term_byのファンクションタグ使用方法

<?php get_term_by( $field, $value, $taxonomy, $output, $filter ) ?>

get_term_byのパラメーター

$field

(string) (required)’slug’, ‘name’, あるいは ‘id’のいずれか。
デフォルト:’id’

(string) (required) Either ‘slug’, ‘name’, or ‘id’
Default: ‘id’

$value

(string|integer) (required) このターム値を検索します。

デフォルト:なし

(string|integer) (required) Search for this term value
Default: None

$taxonomy

(string) (required)category、post_tag、link_categoryあるいはいずれかのcustomのタクソノミー名
デフォルト:なし

(string) (required) Taxonomy Name category, post_tag, link_category or something custom
Default: None

$output

(string) (optional)Constant OBJECT, ARRAY_A, or ARRAY_N
デフォルト: OBJECT

(string) (optional) Constant OBJECT, ARRAY_A, or ARRAY_N
Default: OBJECT

$filter

(string) (optional)デフォルトはrawか、WordPressに定義されたフィルタが適用されません。
デフォルト:’raw’

(string) (optional) default is raw or no WordPress defined filter will applied.
Default: ‘raw’

戻り値

(mixed)

データベースからタームの列(配列)。$taxonomyが存在しないか、$termが見つからない場合はfalseを返します。
返されるフィールドは次のとおりです。

Term Row (array) from database. Will return false if $taxonomy does not exist or $term was not found.
The fields returned are:

  • term_id
  • name
  • slug
  • term_group
  • term_taxonomy_id
  • taxonomy
  • description
  • parent
  • count

Taxonomy_nameは、タクソノミーの名前であり、 必要とされているterm_nameではありません;id (term_id)は、タームのIDであり、post_idではありません;…

Taxonomy_name is the name of taxonomy, not the term_name and is required; the id (term_id) is the ID of the term, not post_id;…

Remember:
↓ Taxonomy type (e.g. post_tag)
Terms in this taxonomy:
→ news
→ webdev
→ …

名前とタクソノミーのタイプによってタームを取得する例(category, post_tag あるいはカスタムタクソノミーとしてtaxonomy_name)。

Examples to get terms by name and taxonomy type (taxonomy_name as category, post_tag or custom taxonomy).

// カテゴリーのタクソノミー内の''news''名でタームを取得します。
//Get term by name ''news'' in Categories taxonomy.
get_term_by('name', 'news', 'category')

// タグタクソノミー内の''news''名でタームを取得します。
// Get term by name ''news'' in Tags taxonomy.
get_term_by('name', 'news', 'post_tag')

// カスタムタクソノミー内の''news''名でタームを取得します。
// Get term by name ''news'' in Custom taxonomy.
get_term_by('name', 'news', 'my_custom_taxonomy')

idで(post_idではなく、term_idで):

By id (term_id, not post_id):

// カテゴリータクソノミー内のid (''term_id'')でタームを取得します。
// Get term by id (''term_id'') in Categories taxonomy.
get_term_by('id', 12, 'category')

...

このページの履歴の中の間違った例

警告:以下の例はが間違っています(このページの履歴を参照):

Warning: the example below is wrong (see in this page history):

get_term_by('id', (int)$post->ID, 'taxonomy_name'); // return null

この例は、タクソノミーのtaxonomy_name内のpost_idとして、ID(term_id)でタームを取得しようとします。このタクソノミーは存在せず、term_idが間違っています。

This example try to get a term with ID (term_id) as post_id and in the taxonomy taxonomy_name. This taxonomy not exists and the term_id is wrong.

これはこの例の現在のバージョンです:

This is the correct version of this example:

// get_term_by('id', category_id, 'category')

$postCategories = get_the_category($post->ID);
foreach ( $postCategories as $postCategory ) {
$myCategories[] = get_term_by('id', $postCategory->cat_ID, 'category');
}
// OR:
$myCategory = get_term_by('id', $postCategories[0]->cat_ID, 'category');
  • 警告: $valueは’name’の$fieldにエスケープしません。必要であれば、それを自分で行う必要があります。
  • ‘get_term_by’$filterのパラムに使用可能な値を示す$contextパラムのリストをsanitize_term_field()で参照してください。
    See sanitize_term_field() The $context param lists the available values for ‘get_term_by’ $filter param.
  • 用途: sanitize_term()は、返す前に$filterコンテキストにもとづいてタームを削除します。
  • グローバル用途: (object) $wpdb
  • Warning: $value is not escaped for ‘name’ $field. You must do it yourself, if required.
  • See sanitize_term_field() The $context param lists the available values for ‘get_term_by’ $filter param.
  • Uses: sanitize_term() Cleanses the term based on $filter context before returning.
  • Uses global: (object) $wpdb

注意

  • Warning: $value is not escaped for ‘name’ $field. You must do it yourself, if required.
  • See sanitize_term_field() The $context param lists the available values for ‘get_term_by’ $filter param.
  • Uses: sanitize_term() Cleanses the term based on $filter context before returning.
  • Uses global: (object) $wpdb
  • Warning: $value is not escaped for ‘name’ $field. You must do it yourself, if required.
  • See sanitize_term_field() The $context param lists the available values for ‘get_term_by’ $filter param.
  • Uses: sanitize_term() Cleanses the term based on $filter context before returning.
  • Uses global: (object) $wpdb

変更ログ

Since: 2.3.0

ソースファイル

get_term_by()は、wp-includes/taxonomy.php内に位置しています。

get_term_by() is located in wp-includes/taxonomy.php.

関連ファンクションタグ

Terms: is_term(), term_exists(), get_objects_in_term(), get_term(), get_term_by(), get_term_children(), get_term_link(), get_terms(), get_the_terms(), get_the_term_list(), has_term(), sanitize term(), wp_get_object_terms(), wp_set_object_terms(), wp_get_post_terms(), wp_set_post_terms()

Sponsored Link