get_taxonomies()
Gets the list of registered taxonomies. You can restrict the list to the desired parameters.
Use get_object_taxonomies() to get taxonomies of specific post_types only.
No Hooks.
Returns
String[]|WP_Taxonomy[].
- Array of names - when $output parameter is 'names'.
- Array of WP_Taxonomy objects - when parameter $output is 'objects'.
Names Return examples:
Array ( [special_taxonomy] => special_taxonomy, [custom_taxonomy] => custom_taxonomy )
Objects Return examples:
Usage
get_taxonomies( $args, $output, $operator );
- $args(array)
An array of
key => valuearguments to match against the taxonomy objects. I.e. by specifying the necessary parameters, we can get data only about the taxonomies we need.Parameters that can be specified in the array:
name- taxonomy name.object_type- (array) to which types of posts the taxonomy is attached.hierarchical- true - will show only tree-like taxonomies.show_in_nav_menuscap- (object) capabilities.label- title (taxonomy name).labels- (object) taxonomy titles (different names).singular_labelshow_uishow_tagcloudpublic- true - get public taxonomies only.update_count_callback.rewritequery_varmanage_capedit_capdelete_capassign_cap_builtin- false - exclude built-in taxonomies (for example, category).
See complete list and what each parameter means in register_taxonomy() function description.
Default: empty array
- $output(string)
The view in which the result of the function will be obtained:
names- Get only names of registered taxonomies (default).objects`- Get an array of objects with full information about the taxonomies.
Default: 'names'
- $operator(string)
The logical operator that defines how to process multiple values of $args parameter. Accepts:
or- selects taxonomies where any of specified $args element is match.and- selects taxonomies where all elements from $args is match.
Default: 'and'
Examples
#1 Let's display all registered taxonomies:
Get an array of names of all registered taxonomies.
$taxonomies = get_taxonomies();
foreach( $taxonomies as $taxonomy ) {
echo '<p>'. $taxonomy. '</p>';
} #2 Get only public taxonomies
Let's display the list of public ('public' => true) taxonomies that are not WP internal ones ('_builtin' => false):
$args = [
'public' => true,
'_builtin' => false
];
$output = 'names'; // or objects
$operator = 'and'; // 'and' or 'or'
$taxonomies = get_taxonomies( $args, $output, $operator );
if( $taxonomies ){
foreach( $taxonomies as $taxonomy ){
echo '<p>'. $taxonomy. '</p>';
}
} #3 Let's get the 'genre' taxonomy
Let's get a taxonomy called 'genre' ('name' => 'genre') and display it's plural name ($taxonomy->labels->name):
$args = [
'name' => 'genre'
];
$output = 'objects'; // or objects
$taxonomies = get_taxonomies( $args, $output );
if( $taxonomies ) {
foreach ( $taxonomies as $taxonomy ) {
echo '<p>' . $taxonomy->labels->name . '</p>';
}
} #4 Get taxonomies of post_type
If you query for taxonomies with a certain post type, for example, as shown below, only the taxonomies associated with that post type will be shown. If a taxonomy is associated with more than one post type, it will not be shown.
$args = [ 'object_type' => [ 'post' ] ]; $taxonomies = get_taxonomies( $args ); print_r( $taxonomies ); /* Array ( [category] => category [post_tag] => post_tag [post_format] => post_format ) */
The alternative is to use get_object_taxonomies().
#5 Get all the taxonomies used in the REST API
$rest_taxes = get_taxonomies( [ 'show_in_rest' => true ], 'names' ); print_r( $rest_taxes ); /* Array ( [category] => category [post_tag] => post_tag [nav_menu] => nav_menu [note_tag] => note_tag ) */
Notes
- Global. WP_Taxonomy[]. $wp_taxonomies The registered taxonomies.
Changelog
| Since 3.0.0 | Introduced. |
get_taxonomies() get taxonomies code WP 6.8.3
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 );
}