Sort WP_Query-> Array of Objects messages conditionally with different headers

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

0
source share
2 answers

You will need to check the values ​​in the array before starting any output.

I would suggest doing this by storing the data that will be displayed in the array, and not immediately print it. Also, assuming $posts same in every foreach , you only need one loop.

Step-by-step example in the comments

 <?php /* declare an array to save the data */ $quarters = array(); foreach($posts as $q) { $donor_date = get_post_meta($q->ID,'donor-date',true); $donor_month = date('m',strtotime($donor_date)); /* format the date once - best practice is not to repeat code */ $formatteddate = date('d/m/Y',strtotime($donor_date)); /* organise the dates by quarter, using the heading as the key for easy display */ if(in_array($donor_month, array('01','02','03'))) $quarters["First Quarter"][] = $formatteddate; else if(in_array($donor_month, array('04','05','06'))) $quarters["Second Quarter"][] = $formatteddate; else if(in_array($donor_month, array('07','08','09'))) $quarters["Third Quarter"][] = $formatteddate; else if(in_array($donor_month, array('10','11','12'))) $quarters["Fourth Quarter"][] = $formatteddate; } /* now go through your array and display the results */ foreach ($quarters as $quartername => $quarter){ /* $quarters won't have any entry for quarters with no dates, so technically this 'if' isn't needed, however I've added it in case it will be needed it for any other changes you make */ if ($quarter){ ?> <h3><?php echo $quartername; ?></h3> <table> <?php foreach ($quarter as $qdate ){ ?> <tr><td> <?php echo $qdate; ?> </td></tr> <?php } // end foreach($quarter...) ?> </table> <?php } // end if } // end foreach($quarters...) ?> 

I'm not sure where your <table> element is created, because at the moment <h3> added between <tr> , which is incorrect, however this reflects the code you posted, so you should know how to work around this yourself!

+1
source

Place the title inside the for loop and check the month with if and else if statements to choose which title display uses one foreach statement

 <?php foreach($posts as $q) { $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)) echo "<h3>First Quarter</h3>"; if(!in_array($donor_month,array('04','05','06'),true) echo "<h3>Second Quarter</h3>"; ?> <tr><td> <?php echo date('d/m/Y',strtotime($donor_date)); ?> </td></tr> <?php endforeach; ?> 
0
source

Source: https://habr.com/ru/post/1272234/


All Articles