Believe it or not, in the current build of WordPress users cannot add categories or tags to items in the Media Library. That shit is cray, amirite? There are plenty of plugins out there that set this up for you, but they’re bloated and not so fun to customize. Plus, coding is fun!

Adding categories or tags to media items is relatively straightforward and I shall show you how! For the sake of this tutorial, I will create a parent category called Authors.

Create a custom taxonomy in functions.php

Calling the function add_author_to_media_taxonomy() will automagically create the taxonomy and also add pages to admin panel in order add and modify them (like categories found under posts or pages).

function add_author_to_media_taxonomy() {
    $labels = array(
        'name'              => 'Add Author',
        'singular_name'     => 'Author',
        'search_items'      => 'Search Authors',
        'all_items'         => 'All Authors',
        'parent_item'       => 'Parent Author',
        'parent_item_colon' => 'Parent Author:',
        'edit_item'         => 'Edit Author',
        'update_item'       => 'Update Author',
        'add_new_item'      => 'Add New Author',
        'new_item_name'     => 'New Author',
        'menu_name'         => 'Authors',
    );
 
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'query_var' => true,
        'rewrite' => true,
        public' => true,
		'show_in_nav_menus' => true,
		'show_ui' => true,
		'show_admin_column' => true,
		'update_count_callback' => '_update_generic_term_count',
    	'publicly_queryable'=>  true,
    	'has_archive'       =>  true,
    	'rewrite' => array('slug' => 'author', 'with_front' => false)
    );
 
    register_taxonomy( 'author', 'attachment', $args );
}
add_action( 'init', 'add_author_to_media_taxonomy' );

Create custom page for displaying items from a certain category

Let’s say we created an author named “Julia Nguyen” and we want to view all of the items categorized under this name.

The link would look something like: http://yoursite.com/author/julia-nguyen

functions.php

function fetch_media_with_taxonomy($parent, $slug) {

		$media = get_posts(array(
		    'showposts' => -1,
		    'post_type' => 'attachment',
		    'tax_query' => array(
		        array(
		        'taxonomy' => $parent,
		        'field' => 'slug',
		        'terms' => array($slug)
		))));

		if (!$media) echo '<p>There are no items.</p>';

		foreach ($media as $medium) {
			echo '<div class="media_item">';
			echo '<a href="/?attachment_id=' . $medium->ID . '">' . $medium->post_title . '</a>';
			echo "<br />Author: ";
			echo get_the_term_list($medium->ID, "author", "", ", ", "" );
			echo "<br />Description: ";
			echo $medium->post_content;
			echo '</div>';
		}
}

taxonomy.php

<?php get_header(); ?>

<?php 
	$term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
	$parent = ucwords(get_query_var('taxonomy'));
?>

<?php echo get_post_type($post); ?>

<h1><?php echo $parent; ?>: <?php echo $term->name; ?></h1>

<p>
	<?php if ($term->count > 1): ?>
	There are <?php echo $term->count; ?> items.
	<?php elseif ($term->count == 1): ?>
	There is <?php echo $term->count; ?> item.
	<?php else: ?>
	There are no items.
	<?php endif; ?>
</p>

<p>
	<?php 
		$parent_id = get_term($parent);
		fetch_media_with_taxonomy(get_query_var('taxonomy'), $term->slug); 
	?>
</p>

<?php get_footer(); ?>

If you noticed, the title of the item is a link to the attachment page. We can also customize the attachment page to display our new categories by adding to the existing single.php.

single.php

<?php if (is_attachment()): ?>
	<h1><?php the_title(); ?></h1>

	<p>
		Posted: <?php the_time('F j, Y') ?>

		<?php the_content(); ?>


		<?php 
			if (taxonomy_exists('author')): 
				echo "<strong>Author(s):</strong>";
				echo get_the_term_list( $post->ID, 'author',' ',', ',''); 
			endif; 
		?>
	</p>

<?php endif; ?>

And you’re done! You have just set up a parent category called Authors.

Related Posts:
Installing Valgrind on OS X El Capitan
Handling the haters
Re-watching Old TV Series

Be bold, take flight - tryangles!