Advertisement
Guest User

Untitled

a guest
Jul 9th, 2012
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. if(defined('SHOW_META_VALUES') || defined('META_ALT_QUERY') || defined('SHOW_META_MULTIPLE')) die('Sorry, we need these defines, you can\'t have them.');
  2. // Whether to show a posts meta values when filtering by meta key
  3. define('SHOW_META_VALUES',true); // Might possibly slow down load times if viewing lots of posts when active
  4. // Option to use alternate query - ymmv, so use whichever works best
  5. define('META_ALT_QUERY',false); // Set to true if you have problems with the standard query, this may help, it may not..
  6. // When filtering by meta key, this sets whether to show all a posts meta values (for that key) or just a single one.
  7. // NOTE: SHOW_META_VALUES must be set to true in order for this to do anything
  8. define('SHOW_META_MULTIPLE',true); // Set to true for multiple or false for single
  9.  
  10. function add_meta_column_head($defaults) {
  11. $defaults['meta'] = ( isset($_GET['meta_key']) && $_GET['meta_key'] != 'All') ? esc_attr($_GET['meta_key']) : '';
  12. return $defaults;
  13. }
  14.  
  15. function add_meta_column($column_name,$post_id) {
  16. $metakey = ( isset($_GET['meta_key']) && $_GET['meta_key'] != 'All') ? esc_attr($_GET['meta_key']) : '';
  17. if( $column_name == 'meta' && '' != $metakey )
  18. $val_num = 1;
  19. echo "\n \t";
  20. if( SHOW_META_MULTIPLE ) {
  21. $meta = '<p>' . $val_num++ . ': '. implode( '</p>' . "\n \t" .'<p>' . $val_num++ . ': ', get_post_meta( $post_id , $metakey , false ) ) . '</p>' . "\n";
  22. }
  23. else {
  24. $meta = '<p>' . $val_num++ . ': '. get_post_meta( $post_id , $metakey , true ) . '</p>' . "\n";
  25. }
  26. echo $meta;
  27. return;
  28. }
  29.  
  30. function wp_admin_filters($query) {
  31. global $pagenow;
  32. if( $query->is_admin && ( 'edit.php' == $pagenow ) ) {
  33.  
  34. $metakey =
  35. ( isset($_GET['meta_key']) && $_GET['meta_key'] != 'All' )
  36. ? esc_attr( $_GET['meta_key'] )
  37. : '';
  38. $sortorder =
  39. ( isset($_GET['order']) && $_GET['order'] == 'asc' )
  40. ? 'asc'
  41. : 'desc';
  42.  
  43. if( '' != $metakey ) {
  44. if( SHOW_META_VALUES ) {
  45. add_filter( 'manage_posts_columns' , 'add_meta_column_head' );
  46. add_action( 'manage_posts_custom_column' , 'add_meta_column' , 2, 2);
  47. }
  48. $query->set( 'orderby' , 'meta_value' );
  49. $query->set( 'meta_key' , $metakey );
  50. }
  51. if( $sortorder != 'desc' ) {
  52. $query->set( 'order' , 'asc' );
  53. }
  54. else {
  55. $query->set( 'order' , 'desc' );
  56. }
  57. }
  58. return $query;
  59. }
  60. function wp_admin_filters_dropdowns() {
  61. global $wpdb;
  62.  
  63. $select_meta = '';
  64. if( !META_ALT_QUERY ) {
  65. $meta_keys = $wpdb->get_col("SELECT DISTINCT meta_key FROM $wpdb->postmeta WHERE SUBSTRING(meta_key,1,1) != '_'" );
  66. }
  67. else {
  68. $meta_keys = $wpdb->get_col("SELECT meta_key FROM wp_postmeta WHERE meta_key NOT LIKE '\_%' GROUP BY meta_key" );
  69. }
  70.  
  71. if( !empty( $meta_keys ) ) {
  72. $metakey = ( isset($_GET['meta_key']) && $_GET['meta_key'] != 'All') ? esc_attr($_GET['meta_key']) : '';
  73. $select_meta .= '<select name="meta_key" id="meta" class="postform">';
  74. $select_meta .= ( $metakey == ( 'All' || '' ) )
  75. ? "\n \t".'<option selected="selected" value="All">' . __('View all meta') . ' &nbsp;&nbsp;</option>'
  76. : "\n \t".'<option value="All">' . __('View all meta') . ' &nbsp;&nbsp;</option>';
  77. foreach($meta_keys as $key => $meta_option) {
  78. $select_meta .= ( $metakey == $meta_option )
  79. ? "\n \t".'<option selected="selected" value="'.$meta_option.'">'.$meta_option.'</option>'
  80. : "\n \t".'<option value="'.$meta_option.'">'.$meta_option.'</option>';
  81. }
  82. $select_meta .= "\n".'</select>'."\n";
  83. }
  84. echo $select_meta;
  85.  
  86. $select_order = '<select name="order">';
  87. $select_order .= "\n \t".'<option value="">&nbsp; --- &nbsp;</option>';
  88. $select_order .= ( isset($_GET['order']) && $_GET['order'] == 'asc' )
  89. ? "\n \t".'<option selected="selected" value="asc">'.__('Ascending').'</option>'
  90. : "\n \t".'<option value="asc">'.__('Ascending').'</option>';
  91. $select_order .= ( isset($_GET['order']) && $_GET['order'] == 'desc' )
  92. ? "\n \t".'<option selected="selected" value="desc">'.__('Descending').'</option>'
  93. : "\n \t".'<option value="desc">'.__('Descending').'</option>';
  94. $select_order .= "\n".'</select>'."\n";
  95.  
  96. echo $select_order;
  97.  
  98. return;
  99. }
  100. add_filter('pre_get_posts', 'wp_admin_filters');
  101. add_action('restrict_manage_posts', 'wp_admin_filters_dropdowns',2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement