I'm working on a site at the moment for an orchestra. The various members need to be listed, according to their instrument. The members have a custom post type of biography and I'm capturing the instrument value via a custom field.
The only way I can figure out how to display the relevant people in their relevant sections is to loop again and again through the custom post type, displaying the people that play a particular instrument by comparing the meta value.
Code looks like this:
<?php $args = array( 'post_type' => 'biographies', 'posts_per_page' => -1 ); ?>
<ul class="no-bull hijax">
<?php $biog = new WP_Query($args);
if( $biog->have_posts() ) : while( $biog->have_posts() ) : $biog->the_post();
$player = get_post_meta($post->ID, 'player', true);
if ($player == 'yes') :
$instrument = get_post_meta($post->ID, 'instrument', true);
if ($instrument == 'violin') :
?>
<li><a id="artist_id_<?php the_ID(); ?>" class="nb" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> : <?php echo($instrument); ?></li>
<?php
endif;
endif;
endwhile; endif;
wp_reset_query();
$biog = new WP_Query($args);
if( $biog->have_posts() ) : while( $biog->have_posts() ) : $biog->the_post();
$player = get_post_meta($post->ID, 'player', true);
if ($player == 'yes') :
$instrument = get_post_meta($post->ID, 'instrument', true);
if ($instrument == 'viola') :
?>
<li><a id="artist_id_<?php the_ID(); ?>" class="nb" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> : <?php echo($instrument); ?></li>
<?php
endif;
endif;
endwhile; endif;
wp_reset_query();
$biog = new WP_Query($args);
if( $biog->have_posts() ) : while( $biog->have_posts() ) : $biog->the_post();
$player = get_post_meta($post->ID, 'player', true);
if ($player == 'yes') :
$instrument = get_post_meta($post->ID, 'instrument', true);
if ($instrument == 'cello') :
?>
<li><a id="artist_id_<?php the_ID(); ?>" class="nb" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> : <?php echo($instrument); ?></li>
<?php
endif;
endif;
endwhile; endif;
wp_reset_query();
etc. etc. ad nauseum. (there are currently 12 loops on the page!!)
This is clearly totally inefficient, but quite simply, I don't know how to write better code that this and need some help!