What about using just get_posts
(removing the order_by
argument), then looping through to create and array of programs, then building the output from that:
$posts = get_posts(array('post_type'=>'programas','meta_key'=>'audio_date'));
$programs = array();
foreach ($posts as $post) {
$days = get_post_meta( $post->ID, 'audio_date', true );
// $time = get_post_meta( $post->ID, 'audio_time', true);
$found = false;
foreach ($days as $day) {
if (!$found) {$programs[$day][] = $post; $found = true;}
}
// foreach ($days as $day) {$programs[$day][$time] = $post;}
}
// start at today, loop for 7 days
$today = date('w');
for ($i = $today; $i < ($today + 7); $i++) {
// fix to the actual day if value is over 6
if ($i > 6) {$day = $i - 7;} else {$day = $i;}
// could do this bit programmatically too
if ($day == 0) {$daytitle = "<b>Sunday</b><br>";}
if ($day == 1) {$daytitle = "<b>Monday</b><br>";}
if ($day == 2) {$daytitle = "<b>Tuesday</b><br>";}
if ($day == 3) {$daytitle = "<b>Wednesday</b><br>";}
if ($day == 4) {$daytitle = "<b>Thursday</b><br>";}
if ($day == 5) {$daytitle = "<b>Friday</b><br>";}
if ($day == 6) {$daytitle = "<b>Saturday</b><br>";}
if (isset($programs[$day])) {
$output .= $daytitle;
foreach ($programs[$day] as $time => $post) {
$output .= $post->post_title;
// $output .= " (".$time.");
$output .= "<br>";
}
}
}
echo $output;
Of course, there may be another level of sorting complexity needed if you wanted to display the program times also, for now I have added some commented lines for that too.