Skip to main content
6 of 6
Minor code correction
t31os
  • 18.7k
  • 3
  • 56
  • 68

You could do it with one loop, you just need a valid sort order right? Players with one instrument, followed by the next and so on..

UPDATE: Following on the asker's comment, you can still use one query and use rewind_posts() to iterate the loop as many times as you need, ie. do something like this to get a custom sort..

<?php
// Add the instruments into the array below, in the order you want them in.
$instruments = array( 'violin', 'viola', 'cello' );

$args = array( 
    'post_type' => 'biographies', 
    'posts_per_page' => -1,
    'nopaging' => true,
    'surpress_filters' => true,
    'meta_query' => array(
        array(
            'key' => 'player',
            'value' => 'yes',
            'compare' => '=',
            'type' => 'CHAR'
        ),
        array(
            'key' => 'instruments',
            'value' => $instruments,
            'compare' => 'IN',
            'type' => 'CHAR'
        )
    ),
); 

$bios = new WP_Query( $args);
?>

<?php if( $bios->have_posts() ) : ?>

    <ul class="no-bull hijax">

    <?php
    foreach( $instruments as $instrument ) :
        
        while( $bios->have_posts() ) : $bios->the_post();
            
            $player_instrument = get_post_meta( get_the_ID(), 'instrument', true );
            
            if( $instrument != $player_instrument )
                continue;
        ?>
        
        <li><a id="artist_id_<?php the_ID(); ?>" class="nb" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> : <?php echo $player_instrument; ?></li>
        
        <?php 
        
        endwhile; 
        rewind_posts();
        
    endforeach;
    wp_reset_query();
    ?>

    </ul>
    
<?php endif; ?>

See if that has the desired effect.. :)

t31os
  • 18.7k
  • 3
  • 56
  • 68