How does one sort by custom comment metadata in the admin panel?

I’ve added a Facebook “like” style feature to comments on my website. So far it’s working great, and I’ve even managed to get it showing in the comments section of the admin panel. However, I’m having a heck of a time trying to figure out how to sort by this new, numeric, custom metadata I’ve made.

I told wordpress I wanted to be able to sort by this value, and this does seem to register:

add_filter( ‘manage_edit-comments_sortable_columns’, ‘my_comments_sortable_columns’ );
function my_comments_sortable_columns( $columns ) {
$columns[‘like_total_count’] = ‘like_total_count’;
return $columns;
}

So that part works. However, I can’t get it to actually sort by this value. After doing some research on similar functionality for post types, this is what I have so far:

add_action( ‘pre_get_comments’, ‘rating_comment_sort_columns_by’);
function rating_comment_sort_columns_by( $query ) {
if( ! is_admin() ) {
// we don’t want to affect public-facing pages
return;
}
$orderby = $query->get( ‘orderby’);
if( ‘like_total_count’ == $orderby ) {
$query->set(‘meta_key’, ‘like_total_count’);
$query->set(‘orderby’,’meta_value_num’);
}
}

I’m at a loss. Please advise.

Read more here:: How does one sort by custom comment metadata in the admin panel?

Leave a Reply

Your email address will not be published. Required fields are marked *