trying to make a custom column sortable in the media library. Found lots of examples for posts, and users but can’t get them to work on the media library page.
function wpse_hook_isv_columns() {
add_action('manage_media_custom_column', 'isv_custom_media_column_content',10,2);
add_filter('manage_media_columns', 'isv_custom_media_column_headings');
add_filter('manage_media_imwidth_sortable_columns', 'imwidth_column_register_sortable' );
}
add_action( 'admin_init', 'wpse_hook_isv_columns' );
function isv_custom_media_column_headings($defaults) {
$defaults['isv_width'] = __( 'Width', 'imwidth' );
$defaults['isv_height'] = 'Height';
return $defaults;
}
function isv_custom_media_column_content($column_name,$id) {
$meta = wp_get_attachment_metadata($id);
switch ($column_name) {
case 'isv_width':
$imWidth = get_post_meta($id, "_im_width", true);
if( $imWidth ) :
echo $imWidth .' ('.$meta['width'].')';
else : // add meta value if not there...
update_post_meta($id, '_im_width', $meta['width']);
echo $meta['width'].' (updated)';
endif;
break;
case 'isv_height':
$desc = get_the_content();
echo $meta['height'] ? $meta['height'] : $none;
break;
}
}
// Register the column as sortable ???
function imwidth_column_register_sortable( $columns ) {
$custom = array(
// meta column id => sortby value used in query
'imwidth' => 'Width',
);
return wp_parse_args($custom, $columns);
}
// This example i found works!
function registerdate($columns) {
$columns['registerdate'] = __('Registered', 'registerdate');
return $columns;
}
add_filter('manage_users_columns', 'registerdate');
function registerdate_columns( $value, $column_name, $user_id ) {
if ( 'registerdate' != $column_name )
return $value;
$user = get_userdata( $user_id );
$registerdate = $user->user_registered;
return $registerdate;
}
add_action('manage_users_custom_column', 'registerdate_columns', 10, 3);
function registerdate_column_sortable($columns) {
$custom = array(
// meta column id => sortby value used in query
'registerdate' => 'registered',
);
return wp_parse_args($custom, $columns);
}
add_filter( 'manage_users_sortable_columns', 'registerdate_column_sortable' );
how can i make isv_width sortable? Any help appreciated!
You cannot do a sort on the attachment meta data specifically because it’s stored in a serialized string.
Whilst
WP_Query
can sort on meta values it can’t sort on data that’s serialized. For examplewp_get_attachment_metadata
fetches and unserializes that for you inside the column callback, but MySQL queries can’t sort on that type of data.Short answer: Not possible due to how the width and height are stored.
Follow-up:
Of course, if you setup the image height or width as seperate meta values you can then query on that, or should be able to at least, using a couple more hook(in addition to what you have)..
manage_upload_sortable_columns
andrequest
.If it helps more, Scribu gave an example of creating new sortable columns here. Note, the hook for the media listing is
upload
and notmedia
for the sortable columns(because the hook is the screen id).I tested this alongside your code and it did work, minus the sort order being off, because i don’t have the
_im_width
key associated with media in my install.I hope that’s enough info to work from, and be sure to check the link to Scribu’s page if you want to see a more complete example of sortable columns.
Full Working Code
Workaround for Updating All Images in the Posts Table
Assembled with the invaluable input from Rarst, Bainternet, kaiser, t31os and scribu throughout this Stack
First: If you’re adding stuff to hooks or filters, you should allways wrap the action calls in a separate function and hook it (in your case:) to
admin_init
:Else you can’t be shure they load early enough.
Also: Why are you calling
$desc = get_the_content();
and not using it?You should also use a string instead of
None
that is sortable and sort before the number “0” or after the letter “Z”. Else it would be – in case of sorting – right in the middle of the sorting results…Sidenote: If you check the Codex about translation, then you’ll see all gettext functions having a second Parameter. That’s the textdomain, nothing else. So your
__('Width','domain');
will probably have your themes/plugins textdomain, hm? 🙂