Display Results of a Specific Taxonomy for a CPT

custom post type loopYou’ve made your custom post type (CPT) and you’ve added a custom taxonomy to your CPT. Now you want to actually display the data in that specific taxonomy on a page. You can do this with a query that is very similar to a standard custom post type loop, but it will have some additional parameters. More specifically, you will need to add in the ‘tax_query’ parameter which accepts an array of data. It is in this where you can specify which taxonomy type you want to pull data from and then the specific taxonomy item or items that you want to display the posts from.

In the example code below, we are working with a custom post type called ‘Resources’. There then was a custom taxonomy called ‘Category Types’ that was added to it. A taxonomy can be thought of as a custom category set. So our ‘Category Types’ taxonomy has a set of potential options that can be added. In our example, this happens to be for a bankruptcy attorney and one of the ‘Category Types’ is ‘Bankruptcy Guides’. This is what we are looping through to find in the example code below and we are using the slug to select the ‘Category Type’, so it is written in its slug form ‘bankruptcy-guides’. There could be many other ‘Category Types’ at any time such as Medical Debt, Tax Repair, Foreclosure, Wage Garnishment, etc., but in this example we only want to pull the results for Bankruptcy Guides.

<?php $query = array( 'post_type' => 'resources',  //your custom post type slug
            'posts_per_page' => '-1',
            'order_by' => 'date', 
            'order' => 'DESC',
                'tax_query' => array(
                        'taxonomy' => 'category_types',  //your general taxonomy slug
                        'field' => 'slug',
                        'terms' => array ('bankruptcy-guides')  //your specific taxonomy item slug
			$args = new WP_Query($query); ?>
			<?php // Make sure $query is not empty. if ( $args->have_posts() ) {

				// Loop over the results.
				while ( $args->have_posts() ) {

<article id="post-<?php the_ID(); ?>" >
						<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
							<?php the_post_thumbnail( 'thumbnail' );?>
						<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
							<?php the_title(); ?>
						<?php the_excerpt(); ?>

					<?php } } //reset global $post wp_reset_postdata(); ?>

To add additional taxonomies to the resulting loop, you would just add additional slugs to the ‘terms’ array like this:

'terms' => array ('bankruptcy-guides','medical-debt','tax-repair') 

If you wanted to display all results, then you don’t need the tax_query at all and I could refer you to the post that just covers a Custom Post Type Loop.

That’s all there is to it. You just need to change the data to match your custom post type that was set up and the taxonomy that was created and linked to that CPT and it will output the data. This output is also very basic and simply shows the linked image, title, and excerpt, so that can always be changed and styled more as well. Now go on and try it out!

Speak Your Mind