lundi 23 mars 2015

Display several random posts, but make sure a condition is met


I have a custom post type for a group of people (employees). At first, I needed to display 7 random employees. That was easy



<?php
$args = array(
'post_type' => 'employees',
'orderby' => 'rand',
'posts_per_page' => 7,
);
$query = new WP_Query($args);
while ( $query->have_posts() ) { $query->the_post();
//do stuff
<?php endif;
} wp_reset_postdata();

?>


But now I've been asked to make sure at least one of the seven is always a woman. The man to woman ratio at the company is 4 to 1.


I've set a checkbox that I can test for:



<?php if( in_array( 'yes', get_field('is_female') ) ){
// this is a female
}


I need help putting it all together. I assume I need to keep track if any of the posts are displayed are women. Once I get to the 7th position if a woman isn't listed, I need to keep iterating until I find one.


Any suggestions?





Aucun commentaire:

Enregistrer un commentaire