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');
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' );
  • Thanks Anton, this is almost working correctly. It's incorrectly ordering the 'online date' column by the first number in the date however (ie dates on the 1st of any month/year appear first), rather than ordering by the month and year. Just as an aside, my dates are formatted as d/m/Y. Does that have any bearing here? – Connor May 1 at 0:24
  • I tried adding $query->set('meta_type', 'date');, but that isn't resulting in the correct ordering either. – Connor May 1 at 0:39
  • 1
    Try to add datetime meta type $query->set('meta_type', 'datetime'); and change orderby value to $query->set('orderby','meta_value_datetime'); – anton May 1 at 7:02
  • Thank Anton, the 'online start' dates are still not ordering correctly though. Is it possible that there's something incorrect in my add_admin_course_column function? – Connor May 1 at 23:38
  • Everything fine there, the problem is that you store date in non SQL format. Exists few more hooks like posts_orderby where you manually need to change sql query, but I'm not familiar with it. If you have a new project, it's better to change your date format to `Y-m-d' for database and display in any format you want. – anton May 2 at 5:28
0
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');
  • The above code will not allow you to change the sorting. Changing the sort by clicking on the table column will stop working, because the field given in the filter will always be used. Question is about sorting by custom field and this will not work for custom field. – nmr Apr 30 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.