admin管理员组文章数量:1431426
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
2 Answers
Reset to default 1The 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');
本文标签: How to Sort Custom Field Admin Column by Date
版权声明:本文标题:How to Sort Custom Field Admin Column by Date 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745538499a2662382.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论