您需要直接扩展MySQL查询,因为您必须使用MySQL函数STR_to_DATE,以便不影响分页等。
您应该能够通过以下方式更改pre\\u get\\u帖子来实现这一点:
add_action( \'pre_get_posts\', \'courses_columns_orderby\' );
function courses_columns_orderby( $query ) {
if( ! is_admin() )
return;
$orderby = $query->get( \'orderby\');
switch( $orderby ){
case \'online_start\':
add_filter( \'posts_join\', \'do_order_by_custom_date_field_join\' );
add_filter( \'posts_orderby\', \'do_order_by_custom_date_field_orderby\',10,2 );
break;
default: break;
}
}
现在,我们手动加入meta\\u表,以便定义它获得的别名:
function do_order_by_custom_date_field_join($join){
global $wpdb;
$join.="LEFT JOIN $wpdb->postmeta my_order_meta ON (($wpdb->posts.ID = my_order_meta.post_id) AND (my_order_meta.meta_key = \'online_start\')) ";
//as we don\'t want our join to be used more than once on this page, we remove the filter
remove_filter( \'posts_join\', \'do_order_by_custom_date_field_join\' );
return $join;
}
最后,我们将订单更改为日期,但使用STR\\u to\\u date函数:
function do_order_by_custom_date_field_orderby($orderby, $wp_query){
$orderby = "STR_TO_DATE(my_order_meta.meta_value,\'%d-%m-%Y\') ".$wp_query->get(\'order\');
remove_filter(\'posts_orderby\', \'do_order_by_custom_date_field_orderby\');
return $orderby;
}
现在,orderby应该可以正常工作了(没有进行测试)
快乐的编码。