You saw the tax_query parameter in Chapter 3 when you looked at the WordPress query. Here you’ll look at it in more depth and go over the different options you have to query for posts using taxonomy information.
What I didn’t mention in the earlier chapter is that since WordPress 3.1 and the introduction of the tax_query parameter, now every query argument relating to taxonomies is filtered through into a tax_query. For example, if you were to take a look at a simple tag query that used the tag__in parameter, it would look like this:
'tag__and' => array( 37, 47 )
However, inside the core files you can see what happens to this query once it reaches the query.php file: if ( !empty($q['tag__and']) ) {
$q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) ); $tax_query[] = array(
'taxonomy' => 'post_tag', 'terms' => $q['tag__and'], 'operator' => 'AND'
Chapter 5 ■ Creating Custom taxonomies and Fields
Since WordPress 3.1, when the tax_query parameter was introduced, the previous method of querying taxonomies using just the taxonomy slug and terms slug has been deprecated (this doesn’t apply to the built-in taxonomies, categories, and tags that still work as shown previously). Currently, it’s still possible to use the old method, and it will return results; however, the deprecation of the method means that at some stage it will be removed from the core code and any remaining queries using that method will no longer work.
So with that in mind, make sure that you know exactly what you’re doing with the tax_query parameter. A simple tax query for one term in a taxonomy would look like this:
'tax_query' => array( array( 'taxonomy' => 'ptd_genre', 'field' => 'slug', 'terms' => 'comedy' ) ),
The tax_query parameter takes its arguments in the form of an array of arrays, so a single taxonomy query requires one nested array with at least the three fields shown here. There are five parameters you can set in total:
• taxonomy: Taxonomy identifier
• field: What to search for; either the term ID or terms slug (defaults to ID) • terms: A string or number for one term and an array if querying for multiple
• include_children: A Boolean for whether to look into the return; also the child terms of the term being queried (defaults to true)
• operator: Database comparison operator for which to compare the terms; can be IN, NOT IN, or AND (defaults to IN).
A full query for a single taxonomy could look something like this: 'tax_query' => array(
array(
'taxonomy' => 'ptd_genre', 'field' => 'slug',
'terms' => array('horror', 'thriller', 'drama'), 'operator' => 'NOT IN',
'include_children' => false, )
)
This code returns movies that are not horror, thriller, or drama films, but would return posts in their children terms, so maybe a comedy horror might slip through.
The real power of tax_query, however, comes when querying for multiple taxonomies. To do this, you pass in multiple arrays constructed in the same manner as you’ve seen before, but with a multitaxonomy query, there’s one more parameter that the tax_query uses: the relation parameter. This argument can take one of two options, which
then dictates how the terms are queried for in the database: either OR or AND, with AND being the default. Therefore, if you were to add a query for the ptd_actor taxonomy to the tax_query, it might look something like this:
'tax_query' => array(
'relation' => 'AND', array(
'taxonomy' => 'ptd_genre', 'field' => 'slug',
'terms' => array('horror', 'thriller', 'drama'), 'operator' => 'NOT IN',
'include_children' => false, ),
array(
'taxonomy' => 'ptd_actor', 'field' => 'slug',
'terms' => array('simon-pegg', 'nick-frost' ), 'operator' => 'AND',
) )
That now gives you the power to really narrow down the query using taxonomies. But besides narrowing down the query by using the AND operator for the relation parameter, you can also use the OR operator to broaden the query between several categories. Here you can query for movies that aren’t a horror, thriller, or drama film; OR star Zooey Deschanel; OR are directed by Seth Rogen or Kevin Smith. Granted, that’s a myriad of unrelated options, but it returns a bunch of movies that someone might want to watch:
'tax_query' => array(
'relation' => 'OR', array(
'taxonomy' => 'ptd_genre', 'field' => 'slug',
'terms' => array('horror', 'thriller', 'drama'), 'operator' => 'NOT IN',
), array(
'taxonomy' => 'ptd_actor', 'field' => 'slug',
'terms' => array( 'zooey-deschanel' ), ),
array(
'taxonomy' => 'ptd_writer', 'field' => 'slug',
'terms' => array( 'seth-rogen', 'kevin-smith' ), ),
)
That was a pretty detailed look at how to use the tax_query option in your WordPress queries; next you’ll see what options you have when it comes to custom templates and displaying posts based on custom taxonomies.
Chapter 5 ■ Creating Custom taxonomies and Fields
Custom Taxonomy Templates
Because taxonomies are a method of grouping your posts, there’s only one lineage of page templates that you can work with, which is part of the archive template route. However, there are many ways to create a template to show content for specific taxonomies down to specific taxonomy terms. The hierarchy for custom taxonomy templates looks like this:
• taxonomy-taxonomy_name-term_name.php; if not available, use • taxonomy-taxonomy_name.php; if not available, use
• taxonomy.php; then finally use
• archive.php (followed by index.php if you want to be thorough)
To translate them into page templates using the genre taxonomy you’ve created, you would need to use the taxonomy identifier you used when you set up the taxonomy and the term slugs; they would look like these:
• taxonomy-ptd_genre-comedy.php • taxonomy-ptd_genre.php
Besides the different custom templates you can use, custom taxonomy information will also be added to the body classes when using the body_class function. This would allow you to use classes in your CSS such as the following:
• tax-ptd_genre (the taxonomy identifier prefixed with tax) • term-comedy (the term slug prefixed with term)
• term-3 (the term ID prefixed with term)
Now you’ve looked at ways of getting posts from WordPress using custom taxonomies, you’ll now look at ways of displaying and using the custom taxonomy information in your templates.