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');