给你,解释作为评论
// setup array to store things in while we loop
$events_out = array();
// assume you already have your loop built the way you want
while($loop->have_posts()) : $loop->the_post();
$instances = array();
// grab meta dates
$instances[] = get_post_meta($post->ID, \'dateA\', true);
$instances[] = get_post_meta($post->ID, \'dateB\', true);
$instances[] = get_post_meta($post->ID, \'dateC\', true);
foreach($instances as $instance){
if(!empty($instance)){
// save into outter array with post_id so we can ref more data
$events_out[] = array(
\'timestamp\' => $instance,
\'post_id\' => $post->ID,
);
}
}
endwhile;
// use array_msort to sort by date! defined below
$events_sorted = array_msort($events_out, array(\'timestamp\' => SORT_ASC));
// output in order, with whatever meta/stuff you want
foreach($events_sorted as $event){
echo \'<div>\';
echo \'<h2>\'.get_the_title($event[\'post_id\']).\'</h2>\';
echo \'<h3>\'.date(\'m/d/Y\', $event[\'timestamp\']).\'</h3>\';
echo get_post_meta($event[\'post_id\'], \'some_meta_stuff\', true);
echo \'<a href="\'.get_permalink($event[\'post_id\']).\'">Read More</a>\';
echo \'</div>\';
}
/**
* array_msort: array_multisort variant
* use: array_msort($array, array(\'name\'=>SORT_DESC, \'cat\'=>SORT_ASC));
*/
function array_msort($array, $cols)
{
$colarr = array();
foreach ($cols as $col => $order) {
$colarr[$col] = array();
foreach ($array as $k => $row) { $colarr[$col][\'_\'.$k] = strtolower($row[$col]); }
}
$eval = \'array_multisort(\';
foreach ($cols as $col => $order) {
$eval .= \'$colarr[\\\'\'.$col.\'\\\'],\'.$order.\',\';
}
$eval = substr($eval,0,-1).\');\';
eval($eval);
$ret = array();
foreach ($colarr as $col => $arr) {
foreach ($arr as $k => $v) {
$k = substr($k,1);
if (!isset($ret[$k])) $ret[$k] = $array[$k];
$ret[$k][$col] = $array[$k][$col];
}
}
return $ret;
}