comment form - How to get top rated posts using wp query?

I want to get top rated posts using wp query. The rating value is store in comment meta database table when some one pos

I want to get top rated posts using wp query. The rating value is store in comment meta database table when some one post a comment on wp post or cpt.Please screenshot

EDIT Here is my code

add_action( 'comment_form_top', 'wpcr_change_comment_form_defaults');
function wpcr_change_comment_form_defaults( ) {


    $star1_title = __('Very bad', 'post-rating');
    $star2_title = __('Bad', 'post-rating');
    $star3_title = __('Meh', 'post-rating');
    $star4_title = __('Pretty good', 'post-rating');
    $star5_title = __('Rocks!', 'post-rating');


    echo '<fieldset class="rating">
    <legend>Rating<span class="required">*</span></legend>
    <input type="radio" id="star5" name="rating" value="5" /><label for="star5" title="'.$star5_title.'">5 stars</label>
    <input type="radio" id="star4" name="rating" value="4" /><label for="star4" title="'.$star4_title.'">4 stars</label>
    <input type="radio" id="star3" name="rating" value="3" /><label for="star3" title="'.$star3_title.'">3 stars</label>
    <input type="radio" id="star2" name="rating" value="2" /><label for="star2" title="'.$star2_title.'">2 stars</label>
    <input type="radio" id="star1" name="rating" value="1" /><label for="star1" title="'.$star1_title.'">1 star</label>
    </fieldset>';

}
//////// save comment meta data ////////
add_action( 'comment_post', 'wpcr_save_comment_meta_data' );

function wpcr_save_comment_meta_data( $comment_id ) {
    $rating =  (empty($_POST['rating'])) ? FALSE : $_POST['rating'];
    add_comment_meta( $comment_id, 'rating', $rating );
}

I want to get top rated posts using wp query. The rating value is store in comment meta database table when some one post a comment on wp post or cpt.Please screenshot

EDIT Here is my code

add_action( 'comment_form_top', 'wpcr_change_comment_form_defaults');
function wpcr_change_comment_form_defaults( ) {


    $star1_title = __('Very bad', 'post-rating');
    $star2_title = __('Bad', 'post-rating');
    $star3_title = __('Meh', 'post-rating');
    $star4_title = __('Pretty good', 'post-rating');
    $star5_title = __('Rocks!', 'post-rating');


    echo '<fieldset class="rating">
    <legend>Rating<span class="required">*</span></legend>
    <input type="radio" id="star5" name="rating" value="5" /><label for="star5" title="'.$star5_title.'">5 stars</label>
    <input type="radio" id="star4" name="rating" value="4" /><label for="star4" title="'.$star4_title.'">4 stars</label>
    <input type="radio" id="star3" name="rating" value="3" /><label for="star3" title="'.$star3_title.'">3 stars</label>
    <input type="radio" id="star2" name="rating" value="2" /><label for="star2" title="'.$star2_title.'">2 stars</label>
    <input type="radio" id="star1" name="rating" value="1" /><label for="star1" title="'.$star1_title.'">1 star</label>
    </fieldset>';

}
//////// save comment meta data ////////
add_action( 'comment_post', 'wpcr_save_comment_meta_data' );

function wpcr_save_comment_meta_data( $comment_id ) {
    $rating =  (empty($_POST['rating'])) ? FALSE : $_POST['rating'];
    add_comment_meta( $comment_id, 'rating', $rating );
}
Share Improve this question edited Jun 8, 2019 at 14:01 Shoaib Saleem asked Jun 6, 2019 at 14:03 Shoaib SaleemShoaib Saleem 1138 bronze badges 3
  • Can you add the code of how you're handling the stars right now? – kero Commented Jun 6, 2019 at 15:45
  • 1 Is that rating field coming from some plugin? – Nilambar Sharma Commented Jun 7, 2019 at 8:17
  • No, I am using custom code to add star rating. – Shoaib Saleem Commented Jun 8, 2019 at 13:19
Add a comment  | 

1 Answer 1

Reset to default 0

Don't worry. I have found solution for your issue. you can use below code to display 5 top rated posts based on average rating. i have created below function to get top average rated post using sql queries and wp_query functions.

here is updated function for all custom post type.

function top_rated_post_via_comment_of_CPT($post_per_page = 5){
 global $wpdb;

    $results = $wpdb->get_results("SELECT DISTINCT(wp_commentsment_post_ID), GROUP_CONCAT(wp_commentsment_iD separator ', ') comment_ids FROM wp_comments JOIN wp_commentmeta ON wp_commentmetament_id = wp_commentsment_ID GROUP BY wp_commentsment_post_ID", ARRAY_A);


    foreach($results as $key => $value) {

        $c_post_id = $value['comment_post_ID'];
        $comment_ids = $value['comment_ids'];
        $res = $wpdb->get_results( "SELECT AVG(`meta_value`) as avg_rate FROM wp_commentmeta WHERE `meta_key` = 'rating' AND comment_ID IN ($comment_ids) ORDER BY meta_value" );
       $results[$key]['avg_rate'] = $res[0]->avg_rate;
    }
    # sort value by high rated
    $avg_rate = array_column($results, 'avg_rate');
    array_multisort($avg_rate, SORT_DESC, $results);

    $top_rated = array();
    foreach ($results as $result) {

        if($result['avg_rate'] && $result['comment_ids'] )
        {
            $top_rated[] = $result['comment_post_ID'];
        }
    }

    $args = array(
        'post_type' => array("post","movies"),
        'posts_per_page' => $post_per_page,
        'post__in' => $top_rated,
        'orderby' => 'post__in' 
    );

    $top_rated_posts = new WP_Query( $args );

    // The Loop
    if ( $top_rated_posts->have_posts() ) {
        echo '<ul>';

        while ( $top_rated_posts->have_posts() ) {
            $top_rated_posts->the_post();
            $new_key = array_search(get_the_id(), array_column($results, 'comment_post_ID'));
            echo '<li>Post Name : ' . get_the_title() . ' | Average Rate :'.number_format((float)$results[$new_key]['avg_rate'], 2, '.', '').'</li>';

        }
        echo '</ul>';

        wp_reset_postdata();
    } else {
        // no posts found
    }
}

echo top_rated_post_via_comment_of_CPT(5);

to add Average column in admin post listing dashboard

 add_filter( 'manage_movies_posts_columns', 'set_custom_edit_columns' );
    add_filter( 'manage_posts_columns', 'set_custom_edit_columns' );
    function set_custom_edit_columns($columns) {
        $columns['avg_rate'] = __( 'Average Rate', 'your_text_domain' );
        return $columns;
    }

// Add the data to the custom columns for the book post type:
add_action( 'manage_posts_custom_column' , 'custom_column', 10, 2 );
add_action( 'manage_movies_custom_column' , 'custom_column', 10, 2 );
function custom_column( $column, $post_id ) {
    switch ( $column ) {

        case 'avg_rate' :
            global  $wpdb;
            $results = $wpdb->get_results("SELECT DISTINCT(wp_commentsment_post_ID), GROUP_CONCAT(wp_commentsment_iD separator ', ') comment_ids FROM wp_comments JOIN wp_commentmeta ON wp_commentmetament_id = wp_commentsment_ID GROUP BY wp_commentsment_post_ID", ARRAY_A);
            foreach($results as $key => $value) {

                $c_post_id = $value['comment_post_ID'];
                $comment_ids = $value['comment_ids'];
                $res = $wpdb->get_results( "SELECT AVG(`meta_value`) as avg_rate FROM wp_commentmeta WHERE `meta_key` = 'rating' AND comment_ID IN ($comment_ids) ORDER BY meta_value" );
               $results[$key]['avg_rate'] = $res[0]->avg_rate;
            }
            $new_key = array_search($post_id, array_column($results, 'comment_post_ID'));
            if($results[$new_key]['avg_rate']){
            echo number_format((float)$results[$new_key]['avg_rate'], 2, '.', '');
            }
            else
            {
                echo "No rating";
            }
            break;
    }
}

i hope this function can solve you query. let me know if this function helps you!

Thank you!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745386233a4625450.html

相关推荐

  • comment form - How to get top rated posts using wp query?

    I want to get top rated posts using wp query. The rating value is store in comment meta database table when some one pos

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信