Using WordPress ‘get_taxonomies()’ PHP function

The get_taxonomies() WordPress PHP function retrieves a list of registered taxonomy names or objects.

Usage

get_taxonomies( $args, $output, $operator )

Example:

$args = array( 'public' => true, '_builtin' => false );
$output = 'names';
$operator = 'and';
$taxonomies = get_taxonomies( $args, $output, $operator );

Parameters

  • $args (array) – Optional. An array of key => value arguments to match against the taxonomy objects. Default: array()
  • $output (string) – Optional. The type of output to return in the array. Accepts either taxonomy ‘names’ or ‘objects’. Default: 'names'
  • $operator (string) – Optional. 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'

More information

See WordPress Developer Resources: get_taxonomies()

Examples

Display a list of only public custom taxonomies

This example will display a list of only public custom taxonomies, excluding built-in WordPress taxonomies (e.g., categories and tags).

$args = array( 'public' => true, '_builtin' => false );
$output = 'names';
$operator = 'and';
$taxonomies = get_taxonomies( $args, $output, $operator );

if ( $taxonomies ) {
  echo '<ul>';
  foreach ( $taxonomies as $taxonomy ) {
    echo '<li>' . $taxonomy . '</li>';
  }
  echo '</ul>';
}

Get taxonomies for a specific post type

This example shows how to get taxonomies connected only with the ‘post’ post type.

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

Display the plural name of a specific taxonomy

This example displays the plural name of the ‘genre’ taxonomy.

$args = array( 'name' => 'genre' );
$output = 'objects';
$taxonomies= get_taxonomies( $args, $output );

if ( $taxonomies ) {
  foreach ( $taxonomies as $taxonomy ) {
    echo '<div>' . $taxonomy->labels->name . '</div>';
  }
}

Display a list of all registered taxonomies

This example displays a list of all registered taxonomies.

$taxonomies = get_taxonomies();

if ( ! empty( $taxonomies ) ) {
  echo '<ul>';
  foreach ( $taxonomies as $taxonomy ) {
    echo '<li>' . $taxonomy . '</li>';
  }
  echo '</ul>';
}

Find the taxonomy from a term slug

This example demonstrates how to find the taxonomy for a given term slug.

$term_slug = 'myterm';
$taxonomies = get_taxonomies();

foreach ( $taxonomies as $tax_type_key => $taxonomy ) {
  // Your logic here
}