Using WordPress ‘get_taxonomies_for_attachments()’ PHP function

The get_taxonomies_for_attachments() WordPress PHP function retrieves all taxonomies that are registered for attachments, including mime-type-specific taxonomies such as attachment:image and attachment:video.

Usage

To use the function, simply call it and specify the output type (either ‘names’ or ‘objects’):

get_taxonomies_for_attachments('output');

Parameters

  • $output (string, optional): The type of taxonomy output to return. Accepts ‘names’ or ‘objects’. Default is ‘names’.

More information

See WordPress Developer Resources: get_taxonomies_for_attachments()

Examples

Displaying attachment taxonomies as a list

This example displays all attachment taxonomies as an unordered list.

$attachment_taxonomies = get_taxonomies_for_attachments('names');

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

Fetching attachment taxonomies as objects

This example retrieves all attachment taxonomies as objects and outputs their labels.

$attachment_taxonomies = get_taxonomies_for_attachments('objects');

foreach ($attachment_taxonomies as $taxonomy) {
    echo $taxonomy->label . '<br>';
}

Checking if a specific taxonomy is registered for attachments

This example checks if the ‘category’ taxonomy is registered for attachments.

$attachment_taxonomies = get_taxonomies_for_attachments('names');

if (in_array('category', $attachment_taxonomies)) {
    echo 'Category taxonomy is registered for attachments.';
} else {
    echo 'Category taxonomy is not registered for attachments.';
}

Counting the number of attachment taxonomies

This example counts the total number of attachment taxonomies and displays the result.

$attachment_taxonomies = get_taxonomies_for_attachments('names');
$count = count($attachment_taxonomies);

echo "There are $count attachment taxonomies.";

Displaying attachment taxonomies with their descriptions

This example outputs attachment taxonomies along with their descriptions.

$attachment_taxonomies = get_taxonomies_for_attachments('objects');

foreach ($attachment_taxonomies as $taxonomy) {
    echo '<strong>' . $taxonomy->label . '</strong>: ' . $taxonomy->description . '<br>';
}