Advertisement
thesufi

Addition on - post table custom column

Feb 7th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.62 KB | None | 0 0
  1. /*
  2. * first paste: http://pastebin.com/wuSwHjjg
  3. */
  4.  
  5. //enable newly added column ordering
  6. add_filter( 'manage_edit-post_sortable_columns', 'dct_table_sorting' ); //use manage_edit-{post_type}_sortable_columns for custom post type
  7. function dct_table_sorting( $columns ) {
  8.   $columns['column1'] = 'column1';
  9.   $columns['column2'] = 'column2';
  10.   return $columns;
  11. }
  12.  
  13. add_filter( 'request', 'dct_column1_orderby' );
  14. function dct_column1_orderby( $vars ) {
  15.     if ( isset( $vars['orderby'] ) && 'column1' == $vars['orderby'] ) {
  16.         $vars = array_merge( $vars, array(
  17.             'meta_key' => 'column1_meta_key',
  18.             'orderby' => 'meta_value'
  19.         ) );
  20.     }
  21.  
  22.     return $vars;
  23. }
  24.  
  25. add_filter( 'request', 'dct_column2_orderby' );
  26. function dct_column2_orderby( $vars ) {
  27.     if ( isset( $vars['orderby'] ) && 'column2' == $vars['orderby'] ) {
  28.         $vars = array_merge( $vars, array(
  29.             'meta_key' => 'column2_meta_key',
  30.             'orderby' => 'meta_value'
  31.         ) );
  32.     }
  33.  
  34.     return $vars;
  35. }
  36.  
  37.  
  38. /*
  39. *re-arranging table columns
  40. *lets add these two columns before date (date is the last column of the list by default)
  41. *if we want to do this, then we need to add following codes inside function "dct_table_head" (see paste: http://pastebin.com/wuSwHjjg) before returning.
  42. */
  43. $key = 'date'; //name of the "date" column key
  44. $offset = array_search($key, array_keys($defaults));
  45. $result = array_merge
  46.     (
  47.         array_slice($defaults, 0, $offset),
  48.         array('column1' => 'Column1', 'column2' => 'Column2'),
  49.         array_slice($defaults, $offset, null)
  50.     );
  51. //we will then return $result, instead of $defaults
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement