Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. // add custom column
  2. add_filter('manage_edit-post_columns', 'my_extra_views_column');
  3. function my_extra_views_column($columns) {
  4. $columns['views'] =__('views');
  5. return $columns;
  6. }
  7.  
  8. // display the content of the custom column
  9. add_action( 'manage_post_posts_custom_column', 'my_cake_column_content', 10, 2 );
  10. function my_cake_column_content( $column_name, $post_id ) {
  11. if ( 'views' != $column_name )
  12. return;
  13.  
  14. echo get_post_views($post_id);
  15. }
  16.  
  17. // add 'views' column to the sortable columns array
  18. add_filter( 'manage_edit-post_sortable_columns', 'my_sortable_views_column' );
  19. function my_sortable_views_column( $columns ) {
  20. $columns['views'] = 'views';
  21.  
  22. return $columns;
  23. }
  24.  
  25. /* Filter The query
  26. *******************************************************************/
  27. // join new table
  28. add_filter('posts_join', 'my_views_table_join_filter');
  29. function my_views_table_join_filter($join) {
  30. global $wp_query, $wpdb;
  31.  
  32. $orderby = $_GET['orderby'];
  33.  
  34. if( ! is_admin() )
  35. return;
  36.  
  37. if( 'views' == $orderby ) {
  38. $join .= "LEFT JOIN `post_views` ON $wpdb->posts.ID = post_views.post_ID ";
  39. }
  40. return $join;
  41. }
  42.  
  43. // add custom field to the select clause
  44. add_filter('posts_fields', 'view_table_fields');
  45. function view_table_fields($fields){
  46. global $wp_query, $wpdb;
  47.  
  48. $orderby = $_GET['orderby'];
  49.  
  50. if( ! is_admin() )
  51. return;
  52.  
  53. if( 'views' == $orderby ) {
  54. $fields .= ", " . "count(post_views.post_ID) AS views";
  55. }
  56. return ($fields);
  57. }
  58.  
  59. // add custom groupby clause
  60. add_filter( 'posts_groupby', 'my_views_table_groupby_filter' );
  61. function my_views_table_groupby_filter($groupby) {
  62. global $wpdb;
  63.  
  64. $orderby = $_GET['orderby'];
  65.  
  66. if( ! is_admin() )
  67. return;
  68.  
  69. if( 'views' == $orderby ) {
  70. $groupby = "{$wpdb->posts}.ID";
  71. }
  72. return $groupby;
  73. }
  74.  
  75. // add custom order clause
  76. add_filter( 'posts_orderby', 'my_views_table_orderby_filter' );
  77. function my_views_table_orderby_filter($orderby_statement) {
  78.  
  79. $orderby = $_GET['orderby'];
  80.  
  81. if( ! is_admin() )
  82. return;
  83.  
  84. if( 'views' == $orderby ) {
  85. $orderby_statement = "views $_GET[order]";
  86. }
  87. return $orderby_statement;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement