I have ratings on a custom comment type that stores the ratings in the comment_karma
meta of the comment. Now I want to pull up the comments for a specific user and sort them by rating. Here is what I have so far:
add_filter( 'pre_get_comments', [ $this, 'filter_comments' ] );
public function filter_comments( $query ) {
// Bail on anything not admin
if ( !is_admin() ) {
return;
}
// Only run this on the comments table
$current_screen = get_current_screen();
if ( 'edit-comments' !== $current_screen->base ) {
return;
}
// Check for user_id
$user_id = isset( $_GET[ 'user_id' ] ) ? absint( $_GET[ 'user_id' ] ) : false;
if ( $user_id ) {
// Order
$order = isset( $_GET[ 'order' ] ) ? sanitize_text_field( $_GET[ 'order' ] ) : 'DESC';
// Set the query
$query->query_vars[ 'type' ] = $this->comment_type;
$query->set( 'user_id', $user_id );
$query->set( 'orderby', 'comment_karma' );
$query->set( 'order', $order );
}
} // End filter_comments()
I am getting the correct comments from the correct user; however, it is not ordering by comment_karma
like I want. I also tried doing the following which didn't help either:
$query->set( 'meta_key', 'comment_karma' );
$query->set( 'orderby', 'meta_key_num' );
Any suggestions?