Took some time to play around and add a little more..
Meta column, custom field data, etc… optional.. (defines at the top – let the user decide if they want it).
if(defined('SHOW_META_VALUES') || defined('META_ALT_QUERY') || defined('SHOW_META_MULTIPLE')) die('Sorry, we need these defines, you can\'t have them.');
// Whether to show a posts meta values when filtering by meta key
define('SHOW_META_VALUES',true); // Might possibly slow down load times if viewing lots of posts when active
// Option to use alternate query - ymmv, so use whichever works best
define('META_ALT_QUERY',false); // Set to true if you have problems with the standard query, this may help, it may not..
// When filtering by meta key, this sets whether to show all a posts meta values (for that key) or just a single one.
// NOTE: SHOW_META_VALUES must be set to true in order for this to do anything
define('SHOW_META_MULTIPLE',true); // Set to true for multiple or false for single
function add_meta_column_head($defaults) {
$defaults['meta'] = ( isset($_GET['meta_key']) && $_GET['meta_key'] != 'All') ? esc_attr($_GET['meta_key']) : '';
return $defaults;
}
function add_meta_column($column_name,$post_id) {
$metakey = ( isset($_GET['meta_key']) && $_GET['meta_key'] != 'All') ? esc_attr($_GET['meta_key']) : '';
if( $column_name == 'meta' && '' != $metakey )
$val_num = 1;
echo "\n \t";
if( SHOW_META_MULTIPLE ) {
$meta = '<p>' . $val_num++ . ': '. implode( '</p>' . "\n \t" .'<p>' . $val_num++ . ': ', get_post_meta( $post_id , $metakey , false ) ) . '</p>' . "\n";
}
else {
$meta = '<p>' . $val_num++ . ': '. get_post_meta( $post_id , $metakey , true ) . '</p>' . "\n";
}
echo $meta;
return;
}
function wp_admin_filters($query) {
global $pagenow;
if( $query->is_admin && ( 'edit.php' == $pagenow ) ) {
$metakey =
( isset($_GET['meta_key']) && $_GET['meta_key'] != 'All' )
? esc_attr( $_GET['meta_key'] )
: '';
$sortorder =
( isset($_GET['order']) && $_GET['order'] == 'asc' )
? 'asc'
: 'desc';
if( '' != $metakey ) {
if( SHOW_META_VALUES ) {
add_filter( 'manage_posts_columns' , 'add_meta_column_head' );
add_action( 'manage_posts_custom_column' , 'add_meta_column' , 2, 2);
}
$query->set( 'orderby' , 'meta_value' );
$query->set( 'meta_key' , $metakey );
}
if( $sortorder != 'desc' ) {
$query->set( 'order' , 'asc' );
}
else {
$query->set( 'order' , 'desc' );
}
}
return $query;
}
function wp_admin_filters_dropdowns() {
global $wpdb;
$select_meta = '';
if( !META_ALT_QUERY ) {
$meta_keys = $wpdb->get_col("SELECT DISTINCT meta_key FROM $wpdb->postmeta WHERE SUBSTRING(meta_key,1,1) != '_'" );
}
else {
$meta_keys = $wpdb->get_col("SELECT meta_key FROM wp_postmeta WHERE meta_key NOT LIKE '\_%' GROUP BY meta_key" );
}
if( !empty( $meta_keys ) ) {
$metakey = ( isset($_GET['meta_key']) && $_GET['meta_key'] != 'All') ? esc_attr($_GET['meta_key']) : '';
$select_meta .= '<select name="meta_key" id="meta" class="postform">';
$select_meta .= ( $metakey == ( 'All' || '' ) )
? "\n \t".'<option selected="selected" value="All">' . __('View all meta') . ' </option>'
: "\n \t".'<option value="All">' . __('View all meta') . ' </option>';
foreach($meta_keys as $key => $meta_option) {
$select_meta .= ( $metakey == $meta_option )
? "\n \t".'<option selected="selected" value="'.$meta_option.'">'.$meta_option.'</option>'
: "\n \t".'<option value="'.$meta_option.'">'.$meta_option.'</option>';
}
$select_meta .= "\n".'</select>'."\n";
}
echo $select_meta;
$select_order = '<select name="order">';
$select_order .= "\n \t".'<option value=""> --- </option>';
$select_order .= ( isset($_GET['order']) && $_GET['order'] == 'asc' )
? "\n \t".'<option selected="selected" value="asc">'.__('Ascending').'</option>'
: "\n \t".'<option value="asc">'.__('Ascending').'</option>';
$select_order .= ( isset($_GET['order']) && $_GET['order'] == 'desc' )
? "\n \t".'<option selected="selected" value="desc">'.__('Descending').'</option>'
: "\n \t".'<option value="desc">'.__('Descending').'</option>';
$select_order .= "\n".'</select>'."\n";
echo $select_order;
return;
}
add_filter('pre_get_posts', 'wp_admin_filters');
add_action('restrict_manage_posts', 'wp_admin_filters_dropdowns',2);
You can sort your posts ascending or descending.
Props to apljdi for the alternate query (added that in as an option).