admin管理员组文章数量:1405516
I have created a number of 'views' column in the admin area of my posts. There are several tutorials for this and I did in fact use a number of these to arrive at my end result.
It works as expected but I would like the initial sort order to be numeric descending, but unfortunately when you first select it, it is always ascending. Clicking it again gives you a descending list. I would like for it to be initially descending when first selected, and then if you want ascending (which is unlikely) I will click it again.
I have tried a number of solutions (all of my own device) but none that work 100% as required.
Here is my initial code which sorts ascending then descending:
add_action( 'pre_get_posts', 'my_views_orderby' );
function my_views_orderby( $query ) {
if( ! is_admin() )
return;
$orderby = $query->get( 'orderby');
if( 'view' == $orderby ) {
$query->set('meta_key','views');
$query->set('orderby','meta_value_num');
}
}
My initial solution was to add a sort order:
add_action( 'pre_get_posts', 'my_views_orderby' );
function my_views_orderby( $query ) {
if( ! is_admin() )
return;
$orderby = $query->get( 'orderby');
if( 'view' == $orderby ) {
$query->set('meta_key','views');
$query->set('orderby','meta_value_num');
$query->set('order','desc');
}
}
This kinda works, but it is ALWAYS descending, since I am setting it that way.
So then I made it conditional:
add_action( 'pre_get_posts', 'my_views_orderby' );
function my_views_orderby( $query ) {
if( ! is_admin() )
return;
$orderby = $query->get( 'orderby');
if( 'view' == $orderby ) {
$query->set('meta_key','views');
$query->set('orderby','meta_value_num');
$orderdir = $query->get( 'order');
if('desc' == $orderdir) {
$query->set('order','asc');
} else {
$query->set('order','desc');
}
}
}
Now this works, but the sort indicator is the wrong way around. ie when it is sorted ascending, the icon is indicating descending and vice versa. When I hover over the sort column, the icon indicates the PRESENT state and not the state it will become. Also, the link url (which appears at the bottom of chrome) is telling me it is going to sort by the incorrect direction. What this means, if I am already sorted by views descending, the link indicates it will sort by descending, and v.v.
In the off chance it's relevant, this is how I have declared the column as sortable:
add_filter( 'manage_edit-post_sortable_columns', 'my_sortable_view_column' );
function my_sortable_view_column( $columns ) {
$columns['post_views'] = 'view';
return $columns;
}
I have created a number of 'views' column in the admin area of my posts. There are several tutorials for this and I did in fact use a number of these to arrive at my end result.
It works as expected but I would like the initial sort order to be numeric descending, but unfortunately when you first select it, it is always ascending. Clicking it again gives you a descending list. I would like for it to be initially descending when first selected, and then if you want ascending (which is unlikely) I will click it again.
I have tried a number of solutions (all of my own device) but none that work 100% as required.
Here is my initial code which sorts ascending then descending:
add_action( 'pre_get_posts', 'my_views_orderby' );
function my_views_orderby( $query ) {
if( ! is_admin() )
return;
$orderby = $query->get( 'orderby');
if( 'view' == $orderby ) {
$query->set('meta_key','views');
$query->set('orderby','meta_value_num');
}
}
My initial solution was to add a sort order:
add_action( 'pre_get_posts', 'my_views_orderby' );
function my_views_orderby( $query ) {
if( ! is_admin() )
return;
$orderby = $query->get( 'orderby');
if( 'view' == $orderby ) {
$query->set('meta_key','views');
$query->set('orderby','meta_value_num');
$query->set('order','desc');
}
}
This kinda works, but it is ALWAYS descending, since I am setting it that way.
So then I made it conditional:
add_action( 'pre_get_posts', 'my_views_orderby' );
function my_views_orderby( $query ) {
if( ! is_admin() )
return;
$orderby = $query->get( 'orderby');
if( 'view' == $orderby ) {
$query->set('meta_key','views');
$query->set('orderby','meta_value_num');
$orderdir = $query->get( 'order');
if('desc' == $orderdir) {
$query->set('order','asc');
} else {
$query->set('order','desc');
}
}
}
Now this works, but the sort indicator is the wrong way around. ie when it is sorted ascending, the icon is indicating descending and vice versa. When I hover over the sort column, the icon indicates the PRESENT state and not the state it will become. Also, the link url (which appears at the bottom of chrome) is telling me it is going to sort by the incorrect direction. What this means, if I am already sorted by views descending, the link indicates it will sort by descending, and v.v.
In the off chance it's relevant, this is how I have declared the column as sortable:
add_filter( 'manage_edit-post_sortable_columns', 'my_sortable_view_column' );
function my_sortable_view_column( $columns ) {
$columns['post_views'] = 'view';
return $columns;
}
Share
Improve this question
asked Dec 29, 2014 at 3:01
MadivadMadivad
2642 silver badges13 bronze badges
1 Answer
Reset to default 2Instead of adding your column label "view"
as a string
, pass it in as an array
with 1
as the second value. Like this: array('view',1)
Full Code:
add_filter( 'manage_edit-post_sortable_columns', 'my_sortable_view_column' );
function my_sortable_view_column( $columns ) {
$columns['post_views'] = array('view',1);
return $columns;
}
I don't have a WP Codex for this, but I found someone else posted a solution here: http://www.weblogmechanic/2014/02/13/making-custom-column-wordpress-admin-sort-reverse-order/
本文标签: initial sort order for a sortable custom column in admin
版权声明:本文标题:initial sort order for a sortable custom column in admin 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744917417a2632079.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论