admin管理员组

文章数量:1122832

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?

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?

Share Improve this question asked Apr 4, 2024 at 18:43 AristoclesAristocles 1397 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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.

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()

本文标签: phpPregetcomments and orderby commentkarma