List of posts by day of the week

I have Radio with programs and they are associated to day of the weak.

Example:

1-Program Disco Nights plays at Monday and Friday

2-Pragram Crazy weekends play only at Sunday

3-etc

4-etc

Objective: List All the programs without repeating the ones are associated to multiple days based on current day and the next ones.

If today is Saturday have to list Saturday programs them sunday, monday etc etc but can’t only be listed once the same program. (THE CLOSEST ONE)

How to achieve this?

On the back-office I have a multi select option which allow user to select multiple days based on plugin framework it stores the meta_value in a serialized array and can not be changed so I have to work with it.

a:3:{i:0;s:1:"3";i:1;s:1:"4";i:2;s:1:"6";}

Each day correspond to a value.

0 – Sunday

1 – Monday

2 – Tuesday

3 – Wednesday

4 – Thursday

5 – Friday

6 – Saturday

Now the hardest part to list it on-front office.

$today = date('w'); 
$args = array(
  'post_type' => 'programas',
  'meta_key' => 'audio_date',
  'orderby' => '  ?? closest one ??',

 $the_query = new WP_Query( $args );

);

Remember meta key returns a serialized array like this

    a:3:{i:0;s:1:"3";i:1;s:1:"4";i:2;s:1:"6";} 

I don’t know how to compare the most closest programs to that day

while ( $the_query->have_posts() ) {
 $the_query->the_post();
 $items = get_post_meta( $post->ID, 'audio_date', true );

the_title();

if ( is_array($items) ) {  
  foreach ( $items as $item) :
    echo $item;
  endforeach; 
 }
}

Any ideas???? I tried also use post_type categories and make one category for each day but its not working also.

2 Answers
2

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.

Leave a Comment