How to Query Custom Posts Using a Custom Taxonomy Term in WordPress

WordPress custom post types, new in version 3, is one of the most exciting enhancements to WordPress to bring it closer to full content management system (CMS) functionality. You can use custom post types to define your own structured content and separate different types of content. Custom post types also introduced custom taxonomies – custom ways of categorizing content. Custom taxonomies can be hierarchical in nature – similar to categories – or freeform – similar to post tags. You can define as many custom taxonomies as you would like, even extending normal posts or pages with new types of taxonomies.

WordPress also introduced ways to query posts by custom taxonomy. This permits you to query specific posts by category or tag – whichever type of custom taxonomy you’ve defined for a custom post type. In this tutorial, we’re going to show you how to query posts within a custom post type, by the custom taxonomy. We’ll break down each line of the code and tell you what we’re doing and at the end provide you all of the code in a single function.

Let’s open our php function on the page:

<?php

We don’t want to interrupt the loop on the page, so we’re going to store the current $post in a temporary variable:

$tempPost = $post;

Next, we’re going to create our query. We have a custom post type setup called books. The custom post type books has a custom taxonomy called books-cat. This is used to define a category-like hierarchical custom taxonomy for us to categorize the books.

This code will query our custom posts for anything in the Action category – our custom taxonomy books-cat.

$actionQ = new WP_Query();

$actionQ->query('posts_per_page=25&post_type=books&show-cats=Action&orderby=title&order=ASC');

while ($actionQ->have_posts()) : $actionQ->the_post();

?>

Now that we have defined our query, we need to then output our posts:

<div class="block">

<?php $image_thumb = get_post_meta($post->ID, 'image-thumb', true); ?>

<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('front-thumbnail'); ?></a>

<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

<div><?php the_excerpt(); ?></div>

</div>

Finally, we end the look and set the $post back to the post that we had in the loop before we created this query.

<?php endwhile;

$post = $tempPost; ?>

Here’s all of the code in a single function:

<?php

$tempPost = $post;

$actionQ = new WP_Query();

$actionQ->query('posts_per_page=25&post_type=books&show-cats=Action&orderby=title&order=ASC');

while ($actionQ->have_posts()) : $actionQ->the_post();

?>

<div>

<?php $image_thumb = get_post_meta($post->ID, 'image-thumb', true); ?>

<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('front-thumbnail'); ?></a>

<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

<div><?php the_excerpt(); ?></div>

</div>

<?php endwhile;

$post = $tempPost; ?>

There are a number of ways you can use this type of query to create really awesome results. Custom taxonomies and custom post types provide some flexibility for your WordPress sites.

Leave a Reply

Your email address will not be published. Required fields are marked *