How to add categories to media attachments in WordPress

If you for example to be able to query images by category for whatever reason, you’ll want to enable category taxonomy for the attachment post type. You ca do that by adding the following 3 lines to your theme’s functions.php file:

function wb_220517_add_categories_to_attachments() {
    register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'wb_220517_add_categories_to_attachments' );
You can download the code for this how-to in
About Pavel Petrov 2 Articles |  21 How-tos
Pavel is a senior developer for the last 7 years, with extended interest in Linux administration, WordPress and Symfony.