我将下面的代码(从您的代码开始)设置为一个短代码,效果很好。当我将方向“ASC”更改为“DESC”时,排序顺序也会更改,因此帖子的顺序是否正确并不“幸运”。它假定日期存储为yyyymmdd。请注意,必须删除该行:
$date = date(\'ddmmyyyy\', strtotime($meta_value));
如果日期已存储在ddmmyyyy。
function sort_by_date() {
$previews_new_loop = array(
\'posts_per_page\' => -1,
\'post_type\' => \'article\', //\'football_match\',
\'meta_key\' => \'kick-off-date\',
\'orderby\' => \'meta_value\',
// \'order\' => \'ASC\',);
\'order\' => \'DESC\',);
$wpquery = new WP_Query($previews_new_loop);
$posts = $wpquery->get_posts();
$ordered_posts = array();
foreach ($posts as $post) {
$meta_value = get_post_meta($post->ID, \'kick-off-date\', true);
if (!$meta_value) {
continue;
}
//$date = date(\'ddmmyyyy\', strtotime($meta_value));
$ordered_posts[$meta_value][] = $post;
}
$html = \'<pre>\'.print_r($ordered_posts, true).\'</pre>\';
return $html;
}
add_shortcode(\'sort_by_date\',\'sort_by_date\');