SYNCER

SYNCER

get_object_taxonomies() - 指定したオブジェクトで利用できるタクソノミーを取得する

公開日:

get_object_taxonomies()は、指定したオブジェクトで利用できるタクソノミーの一覧を、配列で取得する関数です。

構文

array get_object_taxonomies( array|string|WP_Post $object, string $output = 'names' )

パラメータ

$object

オブジェクトの種類を名称、またはオブジェクトで指定する。複数ある場合は配列で指定する。

$output

初期値: 'names'

返り値の形式を指定する。デフォルト(names)ではタクソノミー名の配列が返る。objectsを指定すると、タクソノミーオブジェクトの配列が返る。

"names"
タクソノミー名の配列。
"objects"
タクソノミーオブジェクトの配列。

返り値

array

指定したオブジェクトで利用できるタクソノミーの一覧を配列にした値が返る。各要素は、第2引数の値がnames(初期値)の場合は文字列、objectsの場合はオブジェクトとなる。

関数

この関数内で利用している、別のWordPress関数です。

サンプルコード

投稿

投稿オブジェクト(post)で利用できるタクソノミーの一覧を取得します。

php

<?php
	// 実行
	$result = get_object_taxonomies( "post" ) ;

	// 結果
	print_r( $result ) ;

結果 (出力内容) - PHP7.0.13

Array
(
    [0] => category
    [1] => post_tag
    [2] => post_format
)

固定ページ

固定ページオブジェクト(page)で利用できるタクソノミーの一覧を取得します。

php

<?php
	// 実行
	$result = get_object_taxonomies( "page" ) ;

	// 結果
	print_r( $result ) ;

結果 (出力内容) - PHP7.0.13

Array
(
)

オブジェクトで取得

第2引数に"objects"を指定すると、名前ではなく、タクソノミーオブジェクトの配列を取得できます。

php

<?php
	// 実行
	$result = get_object_taxonomies( "post", "objects" ) ;

	// 結果
	var_dump( $result ) ;

結果 (出力内容) - PHP7.0.13

array(3) {
  ["category"]=>
  object(WP_Taxonomy)#405 (23) {
    ["name"]=>
    string(8) "category"
    ["label"]=>
    string(15) "カテゴリー"
    ["labels"]=>
    object(stdClass)#388 (21) {
      ["name"]=>
      string(15) "カテゴリー"
      ["singular_name"]=>
      string(15) "カテゴリー"
      ["search_items"]=>
      string(24) "カテゴリーを検索"
      ["popular_items"]=>
      NULL
      ["all_items"]=>
      string(21) "カテゴリー一覧"
      ["parent_item"]=>
      string(18) "親カテゴリー"
      ["parent_item_colon"]=>
      string(19) "親カテゴリー:"
      ["edit_item"]=>
      string(24) "カテゴリーの編集"
      ["view_item"]=>
      string(24) "カテゴリーを表示"
      ["update_item"]=>
      string(24) "カテゴリーを更新"
      ["add_new_item"]=>
      string(30) "新規カテゴリーを追加"
      ["new_item_name"]=>
      string(24) "新規カテゴリー名"
      ["separate_items_with_commas"]=>
      NULL
      ["add_or_remove_items"]=>
      NULL
      ["choose_from_most_used"]=>
      NULL
      ["not_found"]=>
      string(51) "カテゴリーが見つかりませんでした。"
      ["no_terms"]=>
      string(21) "カテゴリーなし"
      ["items_list_navigation"]=>
      string(45) "カテゴリーリストナビゲーション"
      ["items_list"]=>
      string(24) "カテゴリーリスト"
      ["menu_name"]=>
      string(15) "カテゴリー"
      ["name_admin_bar"]=>
      string(8) "category"
    }
    ["description"]=>
    string(0) ""
    ["public"]=>
    bool(true)
    ["publicly_queryable"]=>
    bool(true)
    ["hierarchical"]=>
    bool(true)
    ["show_ui"]=>
    bool(true)
    ["show_in_menu"]=>
    bool(true)
    ["show_in_nav_menus"]=>
    bool(true)
    ["show_tagcloud"]=>
    bool(true)
    ["show_in_quick_edit"]=>
    bool(true)
    ["show_admin_column"]=>
    bool(true)
    ["meta_box_cb"]=>
    string(24) "post_categories_meta_box"
    ["object_type"]=>
    array(1) {
      [0]=>
      string(4) "post"
    }
    ["cap"]=>
    object(stdClass)#396 (4) {
      ["manage_terms"]=>
      string(17) "manage_categories"
      ["edit_terms"]=>
      string(15) "edit_categories"
      ["delete_terms"]=>
      string(17) "delete_categories"
      ["assign_terms"]=>
      string(17) "assign_categories"
    }
    ["rewrite"]=>
    array(4) {
      ["with_front"]=>
      bool(true)
      ["hierarchical"]=>
      bool(true)
      ["ep_mask"]=>
      int(512)
      ["slug"]=>
      string(8) "category"
    }
    ["query_var"]=>
    string(13) "category_name"
    ["update_count_callback"]=>
    string(0) ""
    ["_builtin"]=>
    bool(true)
    ["show_in_rest"]=>
    bool(true)
    ["rest_base"]=>
    string(10) "categories"
    ["rest_controller_class"]=>
    string(24) "WP_REST_Terms_Controller"
  }
  ["post_tag"]=>
  object(WP_Taxonomy)#349 (23) {
    ["name"]=>
    string(8) "post_tag"
    ["label"]=>
    string(6) "タグ"
    ["labels"]=>
    object(stdClass)#392 (21) {
      ["name"]=>
      string(6) "タグ"
      ["singular_name"]=>
      string(6) "タグ"
      ["search_items"]=>
      string(15) "タグを検索"
      ["popular_items"]=>
      string(15) "人気のタグ"
      ["all_items"]=>
      string(18) "すべてのタグ"
      ["parent_item"]=>
      NULL
      ["parent_item_colon"]=>
      NULL
      ["edit_item"]=>
      string(15) "タグの編集"
      ["view_item"]=>
      string(15) "タグを表示"
      ["update_item"]=>
      string(15) "タグを更新"
      ["add_new_item"]=>
      string(21) "新規タグを追加"
      ["new_item_name"]=>
      string(15) "新規タグ名"
      ["separate_items_with_commas"]=>
      string(66) "タグが複数ある場合はコンマで区切ってください"
      ["add_or_remove_items"]=>
      string(33) "タグの追加もしくは削除"
      ["choose_from_most_used"]=>
      string(42) "よく使われているタグから選択"
      ["not_found"]=>
      string(42) "タグが見つかりませんでした。"
      ["no_terms"]=>
      string(12) "タグなし"
      ["items_list_navigation"]=>
      string(36) "タグリストナビゲーション"
      ["items_list"]=>
      string(15) "タグリスト"
      ["menu_name"]=>
      string(6) "タグ"
      ["name_admin_bar"]=>
      string(8) "post_tag"
    }
    ["description"]=>
    string(0) ""
    ["public"]=>
    bool(true)
    ["publicly_queryable"]=>
    bool(true)
    ["hierarchical"]=>
    bool(false)
    ["show_ui"]=>
    bool(true)
    ["show_in_menu"]=>
    bool(true)
    ["show_in_nav_menus"]=>
    bool(true)
    ["show_tagcloud"]=>
    bool(true)
    ["show_in_quick_edit"]=>
    bool(true)
    ["show_admin_column"]=>
    bool(true)
    ["meta_box_cb"]=>
    string(18) "post_tags_meta_box"
    ["object_type"]=>
    array(1) {
      [0]=>
      string(4) "post"
    }
    ["cap"]=>
    object(stdClass)#350 (4) {
      ["manage_terms"]=>
      string(16) "manage_post_tags"
      ["edit_terms"]=>
      string(14) "edit_post_tags"
      ["delete_terms"]=>
      string(16) "delete_post_tags"
      ["assign_terms"]=>
      string(16) "assign_post_tags"
    }
    ["rewrite"]=>
    array(4) {
      ["with_front"]=>
      bool(true)
      ["hierarchical"]=>
      bool(false)
      ["ep_mask"]=>
      int(1024)
      ["slug"]=>
      string(3) "tag"
    }
    ["query_var"]=>
    string(3) "tag"
    ["update_count_callback"]=>
    string(0) ""
    ["_builtin"]=>
    bool(true)
    ["show_in_rest"]=>
    bool(true)
    ["rest_base"]=>
    string(4) "tags"
    ["rest_controller_class"]=>
    string(24) "WP_REST_Terms_Controller"
  }
  ["post_format"]=>
  object(WP_Taxonomy)#362 (20) {
    ["name"]=>
    string(11) "post_format"
    ["label"]=>
    string(18) "フォーマット"
    ["labels"]=>
    object(stdClass)#359 (22) {
      ["name"]=>
      string(18) "フォーマット"
      ["singular_name"]=>
      string(18) "フォーマット"
      ["search_items"]=>
      string(15) "タグを検索"
      ["popular_items"]=>
      string(15) "人気のタグ"
      ["all_items"]=>
      string(18) "フォーマット"
      ["parent_item"]=>
      NULL
      ["parent_item_colon"]=>
      NULL
      ["edit_item"]=>
      string(15) "タグの編集"
      ["view_item"]=>
      string(15) "タグを表示"
      ["update_item"]=>
      string(15) "タグを更新"
      ["add_new_item"]=>
      string(21) "新規タグを追加"
      ["new_item_name"]=>
      string(15) "新規タグ名"
      ["separate_items_with_commas"]=>
      string(66) "タグが複数ある場合はコンマで区切ってください"
      ["add_or_remove_items"]=>
      string(33) "タグの追加もしくは削除"
      ["choose_from_most_used"]=>
      string(42) "よく使われているタグから選択"
      ["not_found"]=>
      string(42) "タグが見つかりませんでした。"
      ["no_terms"]=>
      string(12) "タグなし"
      ["items_list_navigation"]=>
      string(36) "タグリストナビゲーション"
      ["items_list"]=>
      string(15) "タグリスト"
      ["menu_name"]=>
      string(18) "フォーマット"
      ["name_admin_bar"]=>
      string(18) "フォーマット"
      ["archives"]=>
      string(18) "フォーマット"
    }
    ["description"]=>
    string(0) ""
    ["public"]=>
    bool(true)
    ["publicly_queryable"]=>
    bool(true)
    ["hierarchical"]=>
    bool(false)
    ["show_ui"]=>
    bool(false)
    ["show_in_menu"]=>
    bool(false)
    ["show_in_nav_menus"]=>
    bool(true)
    ["show_tagcloud"]=>
    bool(false)
    ["show_in_quick_edit"]=>
    bool(false)
    ["show_admin_column"]=>
    bool(false)
    ["meta_box_cb"]=>
    string(18) "post_tags_meta_box"
    ["object_type"]=>
    array(1) {
      [0]=>
      string(4) "post"
    }
    ["cap"]=>
    object(stdClass)#361 (4) {
      ["manage_terms"]=>
      string(17) "manage_categories"
      ["edit_terms"]=>
      string(17) "manage_categories"
      ["delete_terms"]=>
      string(17) "manage_categories"
      ["assign_terms"]=>
      string(10) "edit_posts"
    }
    ["rewrite"]=>
    array(4) {
      ["with_front"]=>
      bool(true)
      ["hierarchical"]=>
      bool(false)
      ["ep_mask"]=>
      int(0)
      ["slug"]=>
      string(4) "type"
    }
    ["query_var"]=>
    string(11) "post_format"
    ["update_count_callback"]=>
    string(0) ""
    ["_builtin"]=>
    bool(true)
  }
}

ソースコード

wp-includes/taxonomy.php

/**
 * Return the names or objects of the taxonomies which are registered for the requested object or object type, such as
 * a post object or post type name.
 *
 * Example:
 *
 *     $taxonomies = get_object_taxonomies( 'post' );
 *
 * This results in:
 *
 *     Array( 'category', 'post_tag' )
 *
 * @since 2.3.0
 *
 * @global array $wp_taxonomies The registered taxonomies.
 *
 * @param array|string|WP_Post $object Name of the type of taxonomy object, or an object (row from posts)
 * @param string               $output Optional. The type of output to return in the array. Accepts either
 *                                     taxonomy 'names' or 'objects'. Default 'names'.
 * @return array The names of all taxonomy of $object_type.
 */
function get_object_taxonomies( $object, $output = 'names' ) {
	global $wp_taxonomies;

	if ( is_object($object) ) {
		if ( $object->post_type == 'attachment' )
			return get_attachment_taxonomies( $object, $output );
		$object = $object->post_type;
	}

	$object = (array) $object;

	$taxonomies = array();
	foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) {
		if ( array_intersect($object, (array) $tax_obj->object_type) ) {
			if ( 'names' == $output )
				$taxonomies[] = $tax_name;
			else
				$taxonomies[ $tax_name ] = $tax_obj;
		}
	}

	return $taxonomies;
}

参考リンク