I have a custom taxonomy called "department"
I have a custom taxonomy called "persontype" (like Administration, Staff, Janitorial)
I have a custom post type called "person"
I need to be able to use custom fields on a page to declare "department" and for each "persontype" list each "person"
I also need to be able to alter the order of "persontype" (perhaps by slug rather than name?)
John Cotton answers:
Something like this will do it:
global $post;
// Get the meta fields for the current page
$meta = get_post_custom($post->ID);
// Get the current page department name
$dept = $meta['your_department_field_name'][0];
// Get a list of all the person types to loop through
$person_types = get_terms('persontype', 'orderby=slug&hide_empty=0');
foreach( $person_types as $type ) {
$args = array(
'post_type' => 'person',
'tax_query' => array(
array( // restrict to the current person type
'taxonomy' => 'persontype',
'field' => 'slug',
'terms' => $type->slug
),
array( // restrict to the current department
'taxonomy' => 'department',
'field' => 'slug',
'terms' => $dept
)
)
);
// Retrieve the posts matching our args
$people = new WP_Query( $args );
if($people) {
echo '<h1>'.$type->name.'</h1>';
foreach( $people as $person ) {
// output however you want to display $person->
}
}
}
Charlie Triplett comments:
Close.
When I add this to the foreach loop output:
<ul>
<li><? the_title(); ?></li>
</ul>
It echos out the Department taxonomy name (dozens of times) rather than the post title. What did I miss?
John Cotton comments:
This bit:
// output however you want to display $person->
the_title is designed for use within a standard loop. My code uses a different query and thus you need to take a slightly different tack.
Without knowing your code, I can't be certain about what's best, but certainly if you use this
<li><?php echo $person->post_title; ?></li>
in place of the the_title, you'll get what you want.
Charlie Triplett comments:
Ahh, okay. This isn't exactly going to take me where I need to go.
I need to run the loop per post — from each post I'm pulling metadata and the featured image.
Example:
<ul>
<li>Name<strong><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php echo $post_meta['firstname'][0]." ".$post_meta['lastname'][0]; ?></a></strong></li>
<li><?php echo $post_meta['title1'][0]; ?></li>
<li><?php echo $post_meta['phone'][0]; ?></li>
<li><a href="mailto:<?php $post_meta['email'][0]; ?>"><?php echo $post_meta['email'][0]; ?></a></li>
</ul>
John Cotton comments:
Sorry, Charlie, you're not making it very clear....
The code I gave you will work with one post. If you want to use it for multiple posts (for example, inside the main loop on a page), the just wrap all my code inside that loop:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
// my code
global $post;
$meta = get_post_custom($post->ID); // ..... etc...
//.... end of my code
<?php endif; ?>
it will work just the same.