• March 28, 2018
  • Development

WordPress: Query Custom Post Types with Custom Taxonomies

Custom taxonomies were introduced in WordPress 3.0 and have created a better way to organize and specifically query custom post types. For more information about what exactly a custom taxonomy is, you can visit the WordPress Codex. This article doesn’t cover how to create a custom taxonomy, but how to use them to enhance your custom post type queries.

Basic Taxonomy Search

Let’s say we have a custom post type of “Vehicles” and have different categories of vehicles under which our post are organized. If we had a taxonomy of “Body Style” and wanted to query items that were categorized as “Sedan”, this is how we would do it:

$args = array (
  'post_type' => 'vehicles',
  'tax_query' => array (
    array (
      'taxonomy' => 'body-style',
      'terms' => 'sedan',
      'field' => 'slug',
    )
  ),
);
Search Multiple Terms in Taxonomy

Now let’s say we want to search for vehicles that are either a “Sedan” or a “Coupe”:

$args = array (
  'post_type' => 'vehicles',
  'tax_query' => array (
    array (
      'taxonomy' => 'body-style',
      'terms' => array('sedan', 'coupe'),
      'field' => 'slug',
    )
  ),
);
Search Multiple Taxonomies

Now a bit more advanced, we want to search for vehicles of a certain body style but also of a certain make.  If we have another custom taxonomy of “vehicle-make” this is how we would put it together:


$args = array (
  'post_type' => 'vehicles',
  'tax_query' => array (
    'relation' => 'AND',
    array (
      'taxonomy' => 'body-style',
      'terms' => array ('sedan', 'coupe'),
      'field' => 'slug',
    ),
    array (
      'taxonomy' => 'vehicle-make',
      'terms' => 'ford',
      'field' => 'slug',
    )
  ),
);

Since we used the AND relationship the search terms of body style AND vehicle-make must be true for a post to show up. So for example, a Chevrolet Sedan or Coupe would not be displayed because it is not under the category of “Ford”.

So what if we wanted to search for vehicles that were either a sedan, coupe or a Ford? We just need to change the relationship to OR:


$args = array (
  'post_type' => 'vehicles',
  'tax_query' => array (
    'relation' => 'OR',
    array (
      'taxonomy' => 'body-style',
      'terms' => array ('sedan', 'coupe'),
      'field' => 'slug',
    ),
    array (
      'taxonomy' => 'vehicle-make',
      'terms' => 'ford',
      'field' => 'slug',
    )
  ),
);
Search For Posts NOT IN a Custom Taxonomy

We can also use our custom taxonomies to eliminate specific posts from our query as well. Let’s say we want to get all vehicles except Ford.

$args = array (
  'post_type' => 'vehicles',
  'tax_query' => array (
    array (
      'taxonomy' => 'vehicle-make',
      'terms' => 'ford',
      'field' => 'slug',
      'operator' => 'NOT IN',
    )
  ),
);

We have just scratched the surface on some ways to use custom taxonomies, but hopefully if you aren’t familiar with using them, this will be a good guide to get you started.