I have an array of objects returned by WP_Query-> posts
array 0 => object(WP_Post) public 'ID' public 'post_title' public 'post_content' 1 => object(WP_Post) public 'ID' public 'post_title' public 'post_content' 2 => object(WP_Post) public 'ID' public 'post_title' public 'post_content' 3 => object(WP_Post) public 'ID' public 'post_title' public 'post_content' 4 => object public 'Donor' public 'date' public 'post_content'
And it goes on ... I'm trying to group this into the first, second, third and fourth quarters based on the date meta field, i.e. the first quarter - the first three months (January, February, March), the second quarter will be the next three and so on and will give the headings "First quarter", "Second quarter", etc .: -
I have achieved this
<h3> First Quarter </h3> <?php foreach($posts as $q1) { $donor_date = get_post_meta($q1->ID,'donor-date',true); $donor_month = date('m',strtotime($donor_date)); if(!in_array($donor_month,array('01','02','03'),true)) continue; ?> <tr><td> <?php echo date('d/m/Y',strtotime($donor_date)); ?> </td></tr> <?php endforeach; ?> <h3> Second Quarter </h3> <?php foreach($posts as $q2) { $donor_date = get_post_meta($q2->ID,'donor-date',true); $donor_month = date('m',strtotime($donor_date)); if(!in_array($donor_month,array('04','05','06'),true)) continue; ?> <tr><td> <?php echo date('d/m/Y',strtotime($donor_date)); ?> </td></tr> <?php endforeach; ?>
etc ... the problem with this approach is that the headers (First quarter, Second quarter ...) will be there, even if there is no data that matches this condition. Is there a way to conditionally show and hide the headers and the best sorting (refactoring) method ........
Thanks in advance........