您需要以可排序的格式存储日期。例如,MySQL日期格式(WordPress在数据库中默认使用的日期格式):
// Convert date to MySQL date format before it is stored in database
$date = date(\'Y-m-d\', \'the_date_value\' );
然后可以按该日期字段排序:
add_action( \'pre_get_posts\', \'closing_column_orderby\' );
function closing_column_orderby( $query ) {
if( ! is_admin() )
return;
$orderby = $query->get( \'orderby\');
if( \'closing\' == $orderby ) {
$query->set(\'meta_key\',\'closing\');
$query->set(\'meta_type\',\'DATE\');
$query->set(\'orderby\',\'meta_value\');
}
}
然后,在显示之前,更改为所需的显示格式。
// Convert from MySQL format to the desired format for display
// For example, use the date format configured in WordPress settings
$date = date( get_option(\'date_format\'), \'the_date_value\' );