【可排序】wp/wordpress新增一列:文章浏览量

//新增点击量并允许排序
add_filter('manage_posts_columns', function($columns){
$columns['views'] = __('点击量');
return $columns;
});

add_action('manage_posts_custom_column',function($column_name,$id){
if ($column_name != 'views'){
return;
}
if ( get_post_meta($id, "views",true) == '' ){
echo '0';
} else {
echo get_post_meta($id, "views",true);
}
},10,2);

add_filter( 'manage_edit-post_sortable_columns', function ( $columns ) {
    $columns['views'] = 'views';
    return $columns;  
});
add_action( 'load-edit.php', function() {  
    add_filter( 'request', 'qui_sort_views' );  
});
function qui_sort_views( $vars ) {     
  if ( isset( $vars['orderby'] ) && 'views' == $vars['orderby'] ) {  
$vars = array_merge(  
$vars,  
array(  
'meta_key' => 'views',  
'orderby' => 'meta_value_num'  
)  
);  
}   
return $vars;  
}
站长推荐:♥紫苏淘免费领取大额优惠券
© 版权声明
THE END
打赏一下吧:1分不嫌少9.9不嫌多,谢谢支持
点赞0赞赏
分享
评论 抢沙发