我有一个自定义的帖子类型名称“Organization”,其中有一个名为“Last Update”的字段。我使用保存此字段的数据
update_post_meta( $company_id, \'profile_updated_on\', current_time( \'timestamp\', 0 ) );
数据库中存储为的数据
meta_key meta_value
1) profile_updated_on 1516763741
2) profile_updated_on 1516691767
3) profile_updated_on 1516691769
现在在自定义帖子类型管理仪表板中,我无法对“上次更新”字段进行排序。我使用下面的代码进行排序,但排序不正确。
// Add the custom columns to the organization post type:
add_filter( \'manage_edit-organization_columns\', \'set_custom_edit_organization_columns\' );
function set_custom_edit_organization_columns($columns) {
$columns[\'profile_updated_on\'] = __( \'Last Updated\', \'cthe\' );
return $columns;
}
// Add the data to the custom columns for the organization post type:
add_action( \'manage_organization_posts_custom_column\' , \'custom_organization_column\', 10, 2 );
function custom_organization_column( $column_name, $post_id ) {
if(\'profile_updated_on\' != $column_name)
return;
$profile_updated_on_timestamp = get_post_meta( $post_id , \'profile_updated_on\' , true );
//echo date_i18n($profile_updated_on);
echo get_date_from_gmt( date( \'Y-m-d \', $profile_updated_on_timestamp ), \'F j, Y\' );
}
function your_columns_head($defaults) {
$new = array();
$updated_by = $defaults[\'profile_updated_on\']; // save the tags column
foreach($defaults as $key=>$value) {
if($key==\'date\') { // when we find the date column
$new[\'profile_updated_on\'] = $updated_by; // put the tags column before it
}
$new[$key]=$value;
}
return $new;
}
add_filter(\'manage_organization_posts_columns\', \'your_columns_head\');
function set_custom_organization_sortable_columns( $columns ) {
$columns[\'profile_updated_on\'] = \'profile_updated_on\';
//To make a column \'un-sortable\' remove it from the array
//unset($columns[\'title\']);
return $columns;
}
add_filter( \'manage_edit-organization_sortable_columns\', \'set_custom_organization_sortable_columns\' );
使用此方法,我得到如下结果:
1) January 23, 2018 22:15
2) January 23, 2018 22:49
3) January 23, 2018 02:16
我也跟着
this link; 但我无法得到确切的结果。
使用pre_get_posts
钩
add_action( \'pre_get_posts\', \'my_organization_orderby\' );
function my_organization_orderby( $query ) {
if( ! is_admin() )
return;
$orderby = $query->get( \'orderby\');
if( \'Last Updated\' == $orderby ) {
$query->set(\'meta_key\',\'profile_updated_on\');
$query->set(\'orderby\',\'meta_value_num\');
}
}
我试过了
meta_value_num
具有
date