最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to Sort Custom Field Admin Column by Date

programmeradmin0浏览0评论

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');
发布评论

评论列表(0)

  1. 暂无评论