QUERY ALL PROGRAMAS - 按未来天数排序
循环一周,直到将来,并存储这些值以减少以后的计算量。
$now = date(\'w\');
$today = intval($now);
$futures = array();
for($offset = 0; $offset < 7; $offset ++) {
if(($next = $today + $offset) > 6) $next -= 7; // day offset
$futures[] = $next;
}
执行查询并收集数据,这些数据将存储到多维数组中,以便稍后排序。
$programs = array();
$posts = get_posts(array(\'post_type\'=>\'programas\',\'meta_key\' => \'audio_date\'));
foreach($posts as $post) {
// deserialize our data for date checks
$dates = maybe_unserialize(get_post_meta($post->ID, \'audio_date\', true));
// check for the next closest date
foreach($futures as $offset => $next) {
// we\'ve found the next closest date
if(in_array($next, $dates)) {
// gather data
$info = array(
\'post\' => $post,
\'ID\' => $post->ID,
\'next\' => $next,
\'offset\' => $offset,
\'dates\' => $dates,
);
// store for later
if( ! isset($programs[ $offset ])) $programs[ $offset ] = array();
$programs[ $offset ][] = $info;
// next date found -- onto the next program
break;
}
}
// (optional) set tags based on dates for future queries
//
// foreach($dates as $day) wp_set_post_tags($post->ID, "day_$day", true);
}
根据节目的日偏移量对节目进行排序
ksort($programs);
现在循环浏览已排序的数据并显示每天
foreach($programs as $offset => $line_up) {
// Name of the day
$day = date(\'l\', strtotime("+{$offset} days"));
echo "<div>$day</div></ul>";
foreach($line_up as $program) {
// prep vars
$title = $program[ \'post\' ]->post_title;
// output visuals
echo "<li>$title</li>";
}
echo "</ul>";
}
<小时>
QUERY DATES BY TAG
如果您在上面的循环或其他地方设置了标记,那么您可以选择基于天进行查询。
// get today and tomorrow
$now = date(\'w\');
$today = intval($now);
$today_tag = "day_$today";
$tomorrow = $today === 6 ? 0 : $today + 1;
$tomorrow_tag = "day_$today";
// do a query for those two days
$posts = get_posts(array(\'post_type\'=>\'programas\',\'meta_key\' => \'audio_date\',\'tag\'=>"$today_tag,$tomorrow_tag"));