Combining custom post types and custom taxonomies in WordPress

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.


&lt;?php	// loop through posts of the custom post type 'client_list'
$loop = new WP_Query( array( 'post_type' =&gt; 'client_list', 'posts_per_page' =&gt; 10 ) ); ?&gt;

&lt;?php while ( $loop-&gt;have_posts() ) : $loop-&gt;the_post(); ?&gt;
&lt;li role="listitem"&gt;
&lt;article class="summary" role="article"&gt;
&lt;header role="heading"&gt;
&lt;h1&gt;&lt;?php the_title(); ?&gt;&lt;/h1&gt;

&lt;?php	the_content();?&gt;

&lt;/header&gt;

<?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)) {
?>

&lt;section&gt;
&lt;header&gt;
&lt;h1&gt;Work for &lt;?php echo $client; ?&gt;&lt;/h1&gt;
&lt;/header&gt;
&lt;ul class="moreLink vMenu" role="menu"&gt;

&lt;?php	// loop through all posts with the custom taxonomy in the 'client_list' which matches client
$workposts = get_posts(array('client_list'=&gt; $client, 'orderby'=&gt;title, 'order'=&gt;ASC, 'posts_per_page' =&gt; -1));
?&gt;

&lt;?php foreach ($workposts as $post) : ?&gt;

&lt;li class="thumb"role="menuitem"&gt;
&lt;a href="&lt;?php the_permalink(); ?&gt;" title="&lt;?php $the_title = get_the_title(); echo $the_title; ?&gt;"&gt;

&lt;?php custom_find_image('imgLeft','h=52&amp;amp;q=75') ?&gt;

&lt;?php echo $the_title; ?&gt;
&lt;/a&gt;
&lt;/li&gt;

&lt;?php	endforeach; // end client projects loop ?&gt;

&lt;/ul&gt;
&lt;/section&gt;

&lt;?php } // end if $client (client projects) not null ?&gt;

&lt;/article&gt;
&lt;/li&gt;
&lt;?php endwhile; // end while have clients ?&gt;

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.

Tags: