Skip to main content
Minor code correction
Source Link
t31os
  • 18.7k
  • 3
  • 56
  • 68
<?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(), 'instruments''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; ?>
<?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(), 'instruments', 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; ?>
<?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; ?>
Fix args indentation
Source Link
t31os
  • 18.7k
  • 3
  • 56
  • 68

Try this and see if it works ok for you.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,
    'meta_key''nopaging' => 'instrument'true,
    'surpress_filters' => true,
    'meta_query' => array(
        array(
            'key' => 'player',
            'value' => 'yes',
            'compare' => '=',
            'type' => 'CHAR'
        ),
        array(
            'key' => 'instrument''instruments',
            // Add all necessary intruments into the'value' below=> array$instruments,
            'value''compare' => array( 'violin', 'viola', 'cello' )'IN',
            'compare''type' => 'IN''CHAR'
        )
    ),
    'orderby' => 'meta_value',
); 

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

<?php if( $bios->have_posts() ) : 
 ?>
    
    <ul class="no-bull hijax"> 

    <?php
    <?phpforeach( $instruments as $instrument ) :
        
        while( $bios->have_posts() ) : $bios->the_post();
            
            $player_instrument = get_post_meta( get_the_ID(), 'instruments', 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 get_post_meta( get_the_ID(), 'instrument', true );$player_instrument; ?></li>
        
        <?php endwhile; ?>
    
     </ul>
    
<?php 
// Only need toendwhile; reset 
 when there's been a call to the the_postrewind_posts();
        
    endforeach;
    wp_reset_query();
    ?>
endif;
    </ul>
    
<?php endif; ?>

Just note, you need to add the applicable instruments intoSee if that has the array on line 14desired effect.. :)

Try this and see if it works ok for you.

<?php
$args = array( 
    'post_type' => 'biographies', 
    'posts_per_page' => -1,
    'meta_key' => 'instrument',
    'meta_query' => array(
        array(
            'key' => 'player',
            'value' => 'yes',
            'compare' => '='
        ),
        array(
            'key' => 'instrument',
            // Add all necessary intruments into the below array
            'value' => array( 'violin', 'viola', 'cello' ),
            'compare' => 'IN'
        )
    ),
    'orderby' => 'meta_value',
); 

$bios = new WP_Query( $args);
if( $bios->have_posts() ) : 
 ?>
    
    <ul class="no-bull hijax">
    
    <?php while( $bios->have_posts() ) : $bios->the_post(); ?>

        <li><a id="artist_id_<?php the_ID(); ?>" class="nb" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> : <?php echo get_post_meta( get_the_ID(), 'instrument', true ); ?></li>
    
   <?php endwhile; ?>
    
     </ul>
    
<?php 
// Only need to reset when there's been a call to the the_post()
wp_reset_query(); 
endif;
?>

Just note, you need to add the applicable instruments into the array on line 14.

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(), 'instruments', 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.. :)

Oops, meta_query noob (fix parameters in meta_query)
Source Link
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..

Try this and see if it works ok for you.

<?php
$args = array( 
    'post_type' => 'biographies', 
    'posts_per_page' => -1,
    'meta_key' => 'instrument',
    'meta_query' => array(
        array(
            'meta_key''key' => 'player',
            'meta_value''value' => 'yes',
            'compare' => '='
        ),
        array(
            'meta_key''key' => 'instrument',
            // Add all necessary intruments into the below array
            'meta_value''value' => array( 'violin', 'viola', 'cello' ),
            'compare' => 'IN'
        )
    ),
    'orderby' => 'meta_value',
); 

$bios = new WP_Query( $args);
if( $bios->have_posts() ) : 
?>
    
    <ul class="no-bull hijax">
    
    <?php while( $bios->have_posts() ) : $bios->the_post(); ?>

        <li><a id="artist_id_<?php the_ID(); ?>" class="nb" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> : <?php echo get_post_meta( get_the_ID(), 'instrument', true ); ?></li>
    
   <?php endwhile; ?>
    
    </ul>
    
<?php 
// Only need to reset when there's been a call to the the_post()
wp_reset_query(); 
endif;
?>

Just note, you need to add the applicable instruments into the array on line 14.

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..

Try this and see if it works ok for you.

<?php
$args = array( 
    'post_type' => 'biographies', 
    'posts_per_page' => -1,
    'meta_key' => 'instrument',
    'meta_query' => array(
        array(
            'meta_key' => 'player',
            'meta_value' => 'yes',
            'compare' => '='
        ),
        array(
            'meta_key' => 'instrument',
            // Add all necessary intruments into the below array
            'meta_value' => array( 'violin', 'viola', 'cello' ),
            'compare' => 'IN'
        )
    ),
    'orderby' => 'meta_value',
); 

$bios = new WP_Query( $args);
if( $bios->have_posts() ) : 
?>
    
    <ul class="no-bull hijax">
    
    <?php while( $bios->have_posts() ) : $bios->the_post(); ?>

        <li><a id="artist_id_<?php the_ID(); ?>" class="nb" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> : <?php echo get_post_meta( get_the_ID(), 'instrument', true ); ?></li>
    
   <?php endwhile; ?>
    
    </ul>
    
<?php 
// Only need to reset when there's been a call to the the_post()
wp_reset_query(); 
endif;
?>

Just note, you need to add the applicable instruments into the array on line 14.

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..

Try this and see if it works ok for you.

<?php
$args = array( 
    'post_type' => 'biographies', 
    'posts_per_page' => -1,
    'meta_key' => 'instrument',
    'meta_query' => array(
        array(
            'key' => 'player',
            'value' => 'yes',
            'compare' => '='
        ),
        array(
            'key' => 'instrument',
            // Add all necessary intruments into the below array
            'value' => array( 'violin', 'viola', 'cello' ),
            'compare' => 'IN'
        )
    ),
    'orderby' => 'meta_value',
); 

$bios = new WP_Query( $args);
if( $bios->have_posts() ) : 
?>
    
    <ul class="no-bull hijax">
    
    <?php while( $bios->have_posts() ) : $bios->the_post(); ?>

        <li><a id="artist_id_<?php the_ID(); ?>" class="nb" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> : <?php echo get_post_meta( get_the_ID(), 'instrument', true ); ?></li>
    
   <?php endwhile; ?>
    
    </ul>
    
<?php 
// Only need to reset when there's been a call to the the_post()
wp_reset_query(); 
endif;
?>

Just note, you need to add the applicable instruments into the array on line 14.

Add explicit compare to first key
Source Link
t31os
  • 18.7k
  • 3
  • 56
  • 68
Loading
Use template tag for post id / add comment re: wp_reset_query
Source Link
t31os
  • 18.7k
  • 3
  • 56
  • 68
Loading
Source Link
t31os
  • 18.7k
  • 3
  • 56
  • 68
Loading