0

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?

2 Answers 2

1

I think you're close with your 2nd snippet, but you should be using meta_value_num instead of meta_key_num.

$query->set( 'meta_key', 'comment_karma' ); 
$query->set( 'orderby', 'meta_value_num' );

See the WP_Query orderby docs for more info.

1
  • I thought it was working with this fix for a second, but I realized it is still sorting by date and not comment_karma.
    – Aristocles
    Commented Apr 4 at 19:28
1

With the help of @f3bruary from the WordPress Chat Discord server, the solution is this:

add_filter( 'posts_orderby', [ $this, 'orderby_comment_karma' ] );
add_filter( 'pre_get_comments', [ $this, 'filter_comments' ] );
/**
 * Order by comment karma
 *
 * @param string $orderby
 * @return string
 */
public function orderby_comment_karma( $orderby ) {
    if ( isset( $_GET[ 'orderby' ] ) && 'comment_karma' == sanitize_key( $_GET[ 'orderby' ] ) ) {
        $orderby = 'comment_karma';
    }
    return $orderby;
} // End orderby_comment_karma()


/**
 * Filter the comments by user id
 *
 * @param object $query
 * @return void
 */
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 ) {

        // Set the query
        $order = isset( $_GET[ 'order' ] ) ? sanitize_text_field( $_GET[ 'order' ] ) : 'DESC';
        $query->set( 'order', $order );
        if ( isset( $query->query[ 'orderby' ] ) && 'comment_karma' == $query->query[ 'orderby' ] ) {
            $query->query_vars[ 'orderby' ] = 'comment_karma';
        }
        // return $query;
    }
} // End filter_comments()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.