Combining custom post types and custom taxonomies in WordPress
- Date:
- 3 Sep 2010
- Category:
So far in this series of posts I’ve outlined the process of creating a custom post type and a set of custom taxonomies. This final post provides an overview of how the two features were combined to build a listings page, incuding converting an existing custom field into a custom taxonomy.
My clients page makes use of a page template, which used to loop through posts in the client category. Now it needs to loop through entries in the custom post type client_list. To recap, previously I created a custom post type loop which looks like this:
<?php // loop through posts of the custom post type 'client_list' $loop = new WP_Query( array( 'post_type' => 'client_list', 'posts_per_page' => 10 ) ); ?> <?php while ( $loop->have_posts() ) : $loop->the_post(); ?> <li role="listitem"> <article class="summary" role="article"> <header role="heading"> <h1><?php the_title(); ?></h1> <?php the_content();?> </header> </li> <?php endwhile; // end while have clients ?>
Now I need to add a list of projects for each of the clients returned by the loop. To do this I first need to see get the custom taxonomy associated with each client, then see if there are any posts associated with that taxonomy term:
<?php // find the array of terms in custom taxonomy for this custom post ID
$client = get_the_term_list( $theID, 'my_client_list', '', '', '' ) ;
// strip the array
$client = strip_tags($client);
// if array contains a client loop
if (!empty($client)) {
?>
Assuming that there are, I then need to loop through all those posts. This works because I’ve associated both a single client and all the projects for that client with a single taxonomy term. So the¬†$client variable is being used to fetch all of the projects for a particular client.¬†The final annotated code is below.
<?php // loop through posts of the custom post type 'client_list'
$loop = new WP_Query( array( 'post_type' => 'client_list', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li role="listitem">
<article class="summary" role="article">
<header role="heading">
<h1><?php the_title(); ?></h1>
<?php the_content();?>
</header>
<?php // find the array of terms in custom taxonomy for this custom post ID
$client = get_the_term_list( $theID, 'my_client_list', '', '', '' ) ;
// strip the array
$client = strip_tags($client);
// if array contains a client loop
if (!empty($client)) {
?>
<section>
<header>
<h1>Work for <?php echo $client; ?></h1>
</header>
<ul class="moreLink vMenu" role="menu">
<?php // loop through all posts with the custom taxonomy in the 'client_list' which matches client
$workposts = get_posts(array('client_list'=> $client, 'orderby'=>title, 'order'=>ASC, 'posts_per_page' => -1));
?>
<?php foreach ($workposts as $post) : ?>
<li class="thumb"role="menuitem">
<a href="<?php the_permalink(); ?>" title="<?php $the_title = get_the_title(); echo $the_title; ?>">
<?php custom_find_image('imgLeft','h=52&amp;q=75') ?>
<?php echo $the_title; ?>
</a>
</li>
<?php endforeach; // end client projects loop ?>
</ul>
</section>
<?php } // end if $client (client projects) not null ?>
</article>
</li>
<?php endwhile; // end while have clients ?>
Converting custom fields to taxonomy terms
At the outset of this particular coding project I was wondering how to either match a custom field to a custom post, convert a custom field into a taxonomy term. Should both these things be impractical the only alternative I could think of was to manually go back though all the work posts and assign a taxonomy term. Fortuitously, out of the search engine results came Scribu’s Custom Field Taxonomies plugin. All I can say is I take my hat of to you sir for providing such a useful utility. It did the leg work faultlessly in seconds. Do backup before giving it a spin though.
And of course, the final result of all this can, at time of writing, be seen here. Hope this little series of posts has been of help.