I think that one of the more underestimated aspects of building solutions with WordPress comes with leveraging custom post types and custom taxonomies.
For those who are currently building products for others – be it plugins, custom themes, or web applications – then you’re likely familiar with how powerful these two features can be.
Specifically, if you’re used to the ideas of models and views, or data objects and a presentation layer (or whatever language your framework of choice uses to describe this information), you can think of custom post types as a bit of a hybrid of models and views.
Similarly, you can think of taxonomies as an easy way to stamp your data – in WordPress, we think of these as categories and tags – but sometimes, I think the terminology may limit us in thinking how we can leverage the features.
Nonetheless, if you’re used to working with WordPress, custom post types, and custom taxonomies, then you’ve likely faced a time when you’ve needed to query information based on a combination of the two.
And though there are a variety of different ways to retrieve custom post types and how they are tagged, here’s one way to grab information that’s stamped with multiple taxonomies.
Get Posts with Multiple Taxonomies
I’ll share more about this particular strategy at the end of the article, but suffice it to say that the general use case behind this is as follows:
- You have a custom post type, say `acme_post_type`
- You’re attempting to retrieve all of the posts of `acme_post_type` that are stamped with both `dangerous` and `toxic`.
- Let’s say that each of these terms have the ID of 100 and 200, respectively.
To do this, you’ll need to understand two things:
- The `relation` argument for taxonomies.
The Codex has a lot of information on this, and in future posts, I’ll likely talk a bit more about this, but in order to actually perform this, you need the following information:
- The custom post type ID (in our case, `acme_post_type`)
- The various IDs of the custom taxonomy term – in our case, `acme_labels` – that you want to retrieve (in our case `100` and `200`).
At this point, it’s a simple matter of basically filling in the information for this query. See the following gist:
Looks easy enough, right?
But for those of you who are new to custom post types, custom taxonomies, and/or custom queries, there are some key things to understand.
Getting The Gist
When it comes to writing code – be it application-level code, database queries, or whatever – I don’t ever think it’s safe to assume that the code makes sense (after all, it is called “code,” right? :).
And although people who have been writing code for quite a while may have an easier time deciphering what’s going on, that’s not always the case especially if this is your first foray into programming or into a new area of a platform or framework.
So, here’s an attempt to break down the above code into plain English:
- The main query is made up of an associative array that consists of the custom post followed by a second array which is identified by the `tax_query` ID which contains a value of an array of arrays.
- The `tax_query` array (or, technically, array of arrays) consists of three arguments: the relationship between the taxonomies being specified, and the terms themselves.
- Because we’re looking for a custom post type that’s stamped with two taxonomy terms, we specify `AND.` If we wanted posts that were tagged with one or the either, then we could specified `OR`. Make sense? There are other operators, as well.
- Next, we provide two arrays – one for each of our custom taxonomy term. Each array consists of three arguments: the ID of the custom taxonomy term (such as `acme_labels`, which field – or attribute – on which we’re querying, which is the `term_ID`, and the value for the term which is 100 and 200.
An attempt to make it even simpler: We’re instructing WordPress to get all of the
acme_post_type posts that are stamped with both
dangerous and the
From this point, you’d then perform the normal Loop operations to check to see if posts exist, and, if so, then iterate through them and render the information to the screen.
Again, there are endless ways in which data can be stored, represented, and stamped using custom post types and taxonomies, and this is but one way to retrieve posts that are stamped with two taxonomies.
There is much more than can be done with these two features that I’ll likely cover in future posts.