函数文档

get_taxonomies()

💡 云策文档标注

概述

get_taxonomies() 函数用于检索已注册的分类法名称或对象列表。它通过参数过滤分类法,支持返回名称数组或对象数组,适用于 WordPress 开发中的分类法管理和查询场景。

关键要点

  • 函数返回已注册分类法的名称或对象数组,可通过 $output 参数控制输出类型('names' 或 'objects')。
  • 使用 $args 参数数组过滤分类法,匹配属性如 name、object_type、public 等,$operator 参数指定逻辑操作('and' 或 'or')。
  • 返回数组结构:名称数组为键值对(如 [special_taxonomy] => special_taxonomy),对象数组包含 hierarchical、labels、cap 等字段。
  • 函数内部调用 wp_filter_object_list() 实现过滤,自 WordPress 3.0.0 版本引入。

代码示例

// 示例:检索公共自定义分类法名称
$args = array(
  'public' => true,
  '_builtin' => false
);
$taxonomies = get_taxonomies( $args, 'names', 'and' );
if ( $taxonomies ) {
  echo '<ul>';
  foreach ( $taxonomies as $taxonomy ) {
    echo '<li>' . $taxonomy . '</li>';
  }
  echo '</ul>';
}

注意事项

  • 当 $args 包含 object_type 参数时,仅返回完全匹配该文章类型的分类法;若分类法关联多个文章类型,可能不被列出,建议使用 get_object_taxonomies() 替代。
  • 返回对象时,可通过 $taxonomy->labels->name 访问分类法标签,但需确保输出类型为 'objects'。
  • 函数在高级用例中可用于根据术语 slug 查找分类法,但需结合 get_term_by() 等函数。

📄 原文内容

Retrieves a list of registered taxonomy names or objects.

Parameters

$argsarrayoptional
An array of key => value arguments to match against the taxonomy objects.

Default:array()

$outputstringoptional
The type of output to return in the array. Either 'names' or 'objects'. Default 'names'.
$operatorstringoptional
The logical operation to perform. Accepts 'and' or 'or'. 'or' means only one element from the array needs to match; 'and' means all elements must match.
Default 'and'.

Return

string[]|WP_Taxonomy[] An array of taxonomy names or objects.

More Information

Parameter $args is an array of key -> value arguments to match against the taxonomies. Only taxonomies having attributes that match all arguments are returned.

  • name
  • object_type (array)
  • label
  • singular_label
  • show_ui
  • show_tagcloud
  • show_in_rest
  • public
  • update_count_callback
  • rewrite
  • query_var
  • manage_cap
  • edit_cap
  • delete_cap
  • assign_cap
  • _builtin

Returned value is an array, a list of taxonomy names or objects. If returning names, you will get an array of the taxonomy names such as

Array ( [special_taxonomy] => special_taxonomy [custom_taxonomy] => custom_taxonomy )

If returning objects, you will get an array of objects such as:

Array ( [special_taxonomy] => stdClass Object  [custom_taxonomy] => stdClass Object  )

wherein each object will contain the following fields:

stdClass Object ( 
[hierarchical] => 
[update_count_callback] =>  
[rewrite] => 
[query_var] => 
[public] => 
[show_ui] => 
[show_tagcloud] => 
[_builtin] => 
[labels] => stdClass Object ( 
	[name] =>  
	[singular_name] =>
	[search_items] => 
	[popular_items] => 
	[all_items] => 
	[parent_item] => 
	[parent_item_colon] =>  
	[edit_item] => 
	[view_item] =>  
	[update_item] => 
	[add_new_item] => 
	[new_item_name] => 
	[separate_items_with_commas] => 
	[add_or_remove_items] => 
	[choose_from_most_used] => 
	[menu_name] =>  
	[name_admin_bar] =>  ) 
[show_in_nav_menus] => 
[cap] => stdClass Object ( 
	[manage_terms] =>  
	[edit_terms] =>  
	[delete_terms] =>  
	[assign_terms] =>  ) 
[name] =>  
[object_type] => Array () 
[label]  )

Source

function get_taxonomies( $args = array(), $output = 'names', $operator = 'and' ) {
	global $wp_taxonomies;

	$field = ( 'names' === $output ) ? 'name' : false;

	return wp_filter_object_list( $wp_taxonomies, $args, $operator, $field );
}

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes

  1. Skip to note 8 content

    Display a list of only public custom taxonomies — will not list include WordPress built-in taxonomies (e.g., categories and tags):

     true,
      '_builtin' => false
      
    ); 
    $output = 'names'; // or objects
    $operator = 'and'; // 'and' or 'or'
    $taxonomies = get_taxonomies( $args, $output, $operator ); 
    if ( $taxonomies ) {
    	echo '<ul>';
    	foreach ( $taxonomies  as $taxonomy ) {
    		echo '<li>' . $taxonomy . '</li>';
    	}
    	echo '</ul>';	
    }
    ?>

  2. Skip to note 9 content

    When querying the taxonomies with a specific post type like “posts” in this case, only the taxonomies connected only with this post type will be shown. If a taxonomy is attached to multiple post types it will not be listed. Core Trac Ticket 27918

     array(
    		'post',
    	),
    );
    $taxonomies = get_taxonomies( $args );

    The alternative is to use get_object_taxonomies().

  3. Skip to note 10 content

    Display the plural name of a specific taxonomy (and setting the output to be 'object' rather than the default 'array‘):

     'genre'
    );
    $output = 'objects'; // or names
    $taxonomies= get_taxonomies( $args, $output ); 
    if ( $taxonomies ) {
    	foreach ( $taxonomies as $taxonomy ) {
    		echo '<div>' . $taxonomy->labels->name . '</div>';
    	}
    }  
    ?>

  4. Skip to note 12 content

    This can be used in certain use cases as if you need to find the taxonomy from a term slug

                    // We want to find the Taxonomy to this slug.
                    $term_slug = 'myterm';
                    $taxonomies = get_taxonomies();
                    foreach ( $taxonomies as $tax_type_key => $taxonomy ) {
                        // If term object is returned, break out of loop. (Returns false if there's no object)
                        if ( $term_object = get_term_by( 'slug', $term_slug , $taxonomy ) ) {
                            break;
                        }
                    }
    				//Get the taxonomy!!
                    echo $term_object->taxonomy . '<br>';
    
    				// You can also retrieve other thing of the term:
                    echo $term_object->name . '<br>'; //term name
                    echo $term_object->term_id . '<br>'; // term id
                    echo $term_object->description . '<br>'; // term description
    
    				// See all options by dumping the $term_object:
    				//var_dump( $term_object );

  5. Skip to note 14 content

    Get only public taxonomies by post type that are displayed on the admin nav menu

        $args       = array(
            'object_type' => array( 'post' ),
            'public'      => true,
            'show_ui'     => true,
        );
        $taxonomies = get_taxonomies( $args, 'object' );
        $response   = array();
        foreach ( $taxonomies as $taxonomy ) {
            array_push(
                $response,
                array(
                    'name'  => $taxonomy->name,
                    'label' => $taxonomy->label,
                )
            );
        }
    
        print_r( $response );

    The typical response will look like this:

    [
        {
            "name": "category",
            "label": "Categories"
        },
        {
            "name": "post_tag",
            "label": "Tags"
        }
    ]