How to Output Custom Taxonomies with the Custom Posts Belonging to Each One
I recently encountered a small problem while developing a WordPress website. Here’s the scenario:
I have a custom post type called: staff_members. I have also created Custom post types under that which included names of people with their biographies. In the mean time, I have a custom taxonomy called: departments (highlighted in bold below)
The problem was…
how do I output a list of all taxonomies attached to a the custom post type, with a list under each taxonomy of the custom posts under each one. In other words, how do I output the following?
Quality Control Department
- John Smith
- David Feldman
Financial Department
- Mark Robinson
- Richard Carpenter
- Sarah Fisherman
Media Department
- James Shelver
- Alfred Kane
After a bit of research I found this excellent code at WordPress Help which I thought I should share with everyone having the same problem.
I wish I remembered to bookmark the post but extortionately I didn’t
Code snippet to list custom taxonomies with the custom posts under each one:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //for a given post type, return all taxonomies attached to it $post_type = 'staff_members'; $tax = 'departments'; $tax_terms = get_terms($tax); if ($tax_terms) { foreach ($tax_terms as $tax_term) { $args=array( 'post_type' => $post_type, "$tax" => $tax_term->slug, 'post_status' => 'publish', 'posts_per_page' => -1, 'caller_get_posts'=> 1 ); $my_query = null; $my_query = new WP_Query($args); if( $my_query->have_posts() ) { echo ' <h3>'.$tax_term->name.'</h3> '; while ($my_query->have_posts()) : $my_query->the_post(); ?> <a href="<?php the_permalink();?>"><!--?php the_title();?--></a> <!--?php endwhile; } wp_reset_query(); } } ?--> |
So go ahead, and try it out and if you have any questions, kindly leave them at the comments below
Continue reading with more related articles:
Other Related Articles:
- WordPress as a CMS with the powerful Custom Post Types
- The 10 most important features in WordPress 3.1
- New Series: How do I build this awesome website using WordPress?
- 7 Features to look forward to in WordPress 3.0
- WordPress 3.0 Thelonious: Multi Blogs, Single Installation, More Customization




