How to Sort Custom Field Admin Column by Date

I have a WordPress website with a custom post type called 'Courses'. On the WordPress dashboard page listing e

I have a WordPress website with a custom post type called 'Courses'.

On the WordPress dashboard page listing each course I've added a new column displaying a custom field, which is a date, called 'online start'. The online start date is a manually specified date that has no relation to the publish date of the post.

I'd like to be able to sort the list of courses by the date now displayed in the 'online start' column.

I have added the sort functionality, as included in the add_sortable_date_column function below, but the dates are not sorting in the correct order. I believe this is because I need to format the $onlinestart variable as a date, which I'm not quite sure how to do.

Any help would be much appreciated.

//add custom field column to post list
function add_admin_course_column_title( $columns ) {
  $columns['online_start'] = __( 'Online Start' );
  return $columns;
}
add_filter( 'manage_courses_posts_columns', 'add_admin_course_column_title' );

function add_admin_course_column( $column, $post_id ) {
    if ( 'online_start' === $column ) {
        $onlinestart = get_post_meta( $post_id, 'online_start', true );

        if ( ! $onlinestart ) {
                _e( 'n/a' );
        } 
        else {
            echo $onlinestart;
        }
    }
}
add_action( 'manage_courses_posts_custom_column', 'add_admin_course_column', 10, 2);

function add_sortable_date_column( $columns ) {
  $columns['online_start'] = 'online_start';
  return $columns;
}
add_filter( 'manage_edit-courses_sortable_columns', 'add_sortable_date_column');

I have a WordPress website with a custom post type called 'Courses'.

On the WordPress dashboard page listing each course I've added a new column displaying a custom field, which is a date, called 'online start'. The online start date is a manually specified date that has no relation to the publish date of the post.

I'd like to be able to sort the list of courses by the date now displayed in the 'online start' column.

I have added the sort functionality, as included in the add_sortable_date_column function below, but the dates are not sorting in the correct order. I believe this is because I need to format the $onlinestart variable as a date, which I'm not quite sure how to do.

Any help would be much appreciated.

//add custom field column to post list
function add_admin_course_column_title( $columns ) {
  $columns['online_start'] = __( 'Online Start' );
  return $columns;
}
add_filter( 'manage_courses_posts_columns', 'add_admin_course_column_title' );

function add_admin_course_column( $column, $post_id ) {
    if ( 'online_start' === $column ) {
        $onlinestart = get_post_meta( $post_id, 'online_start', true );

        if ( ! $onlinestart ) {
                _e( 'n/a' );
        } 
        else {
            echo $onlinestart;
        }
    }
}
add_action( 'manage_courses_posts_custom_column', 'add_admin_course_column', 10, 2);

function add_sortable_date_column( $columns ) {
  $columns['online_start'] = 'online_start';
  return $columns;
}
add_filter( 'manage_edit-courses_sortable_columns', 'add_sortable_date_column');
Share Improve this question asked Apr 30, 2019 at 9:21 ConnorConnor 31 silver badge7 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 1

The function add_sortable_date_column only registers columns as sortable.
Add one more function to tell wordpress how to sort your columns.

function courses_columns_orderby( $query ) {

    if( ! is_admin() )
        return;

    $orderby = $query->get( 'orderby');

    switch( $orderby ){
        case 'online_start': 
            $query->set('meta_key','online_start');
            $query->set('orderby','meta_value');
            break;
        default: break;
    }

}

add_action( 'pre_get_posts', 'courses_columns_orderby' );
function post_types_admin_order( $wp_query ) {
  if (is_admin()) {

   $post_type = $wp_query->query['post_type'];

  if ( $post_type == 'your post type name') { //like post 

    $wp_query->set('orderby', 'column table header tag id'); //like comments

    $wp_query->set('order', 'DESC'); //change order
   }

  }

 }
 add_filter('pre_get_posts', 'post_types_admin_order');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745538500a4632016.html

相关推荐

  • How to Sort Custom Field Admin Column by Date

    I have a WordPress website with a custom post type called 'Courses'. On the WordPress dashboard page listing e

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信