1. Code
  2. Creative Coding

Advanced WordPress Attachments: Categories and Taxonomy Terms

This post is part of a series called Advanced Use of Attachments in WordPress.
Advanced WordPress Attachments: Creating Custom Queries

As mentioned in the article teaser, this tutorial is the first in a four part series in which you'll learn some techniques for working with images in attachments in WordPress which give you advanced options.

In this series, I'll cover:

  • assigning categories and taxonomies to attachments,
  • querying media files by category/taxonomy so you can output them in a custom loop,
  • using taxonomy/category queries for images to display them on archive pages
  • adding an image to a category or taxonomy term as the category or term's 'featured image'

Assigning categories, tags or terms to attachments is a technique that isn't immediately obvious, but attachments are just a post type like any other, so the concept is relatively easy to grasp. Doing this gives you a range of options for managing images and media files in your site and displaying them to users.

Some examples of how you might use this include:

  • You may create a taxonomy for documents and assigning terms to documents as you upload them, then creating a custom taxonomy template file to display a list of documents with a particular term which might be useful for a site which is being used a document management repository.
  • Perhaps you may use a taxonomy to display images with particular terms in a custom taxonomy template file, thus creating galleries without having to go to the lengths of manually creating them.
  • Maybe you have the need to assign a category, tag or taxonomy term to an image to effectively make that image the featured image for the term enabling you to display it in the archive for that term.

The starting point is to create a taxonomy for attachments and assign it to them, so I'll show you how to do that in this tutorial. I'm going to create two taxonomies: one for documents and another for images.

Note: If you want to apply existing categories and tags to your media, see my tutorial on assigning categories and tags to attachments.

What You'll Need

To follow this tutorial you'll need the following:

  • a development installation of WordPress
  • FTP access (or MAMP or similar if you're working locally)
  • a code editor

I'm going to create a plugin for this tutorial as registering taxonomies is theme-independent - but you might choose to put the code in your theme's functions file.

Setting up the Plugin

First, I'll start by setting up my plugin:

Now I'll create a function for my taxonomies, which I'll hook into the init hook:

The next step is to populate this function with the code to register my taxonomies, using the register_taxonomy() function.

Registering the Taxonomies

First, I'll register a taxonomy for documents. Add the following inside the wptp_register_attachments_tax() function:

Next, let's register the taxonomy for galleries. Add the following below the register_taxonomy() function for the Document Categories taxonomy, still inside the wptp_register_attachments_tax() function:

This adds both taxonomies to the media menu:


Adding Media and Assigning Taxonomy Terms

The final step is to upload some media files and assign taxonomy terms to them. Until you have a range of documents and media with different categories, do this using the WordPress Media Manager.

Because I added the show_admin_column argument to my register_taxonomy() functions, I can see the categories displayed in the Media Library screen. 

Here are the images:



Assigning taxonomies to attachments is straightforward, and gives you a range of ways you can then display your attachments to users. In this tutorial you've learned how to create the taxonomies and apply them to attachments.

In the next tutorial, I'll demonstrate how to create a custom template file to display attachments using the Document Categories taxonomy created in this tutorial.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.