当サイト、Codex 日本語版は今後積極的な更新は行わない予定です。後継となる新ユーザーマニュアルは、https://ja.wordpress.org/support/ にあります。
万が一、当サイトで重大な問題を発見した際などは、フォーラムWordSlack #docs チャンネルでお知らせください。</p>

関数リファレンス/get term by

提供: WordPress Codex 日本語版
< 関数リファレンス
2014年7月22日 (火) 00:06時点におけるGblsm (トーク | 投稿記録)による版 (Notes)

移動先: 案内検索

このページ「関数リファレンス/get term by」は未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています

Description

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

Warning: $value is not HTML-escaped for the '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.

Usage

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

Parameters

$field
string) (required) Either 'id', 'slug', 'name', or 'term_taxonomy_id'
初期値: 'id'
$value
string|integer) (必須) Search for this term value
初期値: なし
$taxonomy
string) (必須) Taxonomy Name category, post_tag, link_category or something custom
初期値: なし
$output
string) (optional) Constant OBJECT, ARRAY_A, or ARRAY_N
初期値: OBJECT
$filter
string) (optional) default is raw or no WordPress defined filter will applied.
初期値: 'raw'

Return Values

(mixed) 
Term Row (object or array) from database. Will return false if $taxonomy does not exist or $term was not found. Othewise returns object (by default) or array of fields depending on $output parameter.

The fields returned are:

  • term_id (See warning below)
  • name
  • slug
  • term_group
  • term_taxonomy_id
  • taxonomy
  • description
  • parent
  • count

Warning: string vs integer confusion! Field values, including term_id are returned in string format. Before further use, typecast numeric values to actual integers, otherwise WordPress will mix up term_ids and slugs which happen to have only numeric characters!

Examples

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
→ ...

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

// Get term by name ''news'' in Categories taxonomy.
get_term_by('name', 'news', 'category')

// Get term by name ''news'' in Tags taxonomy.
get_term_by('name', 'news', 'post_tag')

// Get term by name ''news'' in Custom taxonomy.
get_term_by('name', 'news', 'my_custom_taxonomy')

By id (term_id, not post_id):

// Get term by id (''term_id'') in Categories taxonomy.
get_term_by('id', 12, 'category')

...


Wrong example in this page history

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

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

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

Notes

  • 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

Change Log

Since: 2.3.0

Source File

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

Related

タームタグ: 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(), the_terms(), get_object_taxonomies() is_object_in_taxonomy() the_taxonomies() wp_get_object_terms(), wp_set_object_terms(), wp_get_post_terms(), wp_set_post_terms(), wp_delete_object_term_relationships()



関数リファレンステンプレートタグ目次もご覧ください。


最新英語版: WordPress Codex » Function Reference/get term by最新版との差分