• Resolved maheraldous

    (@maheraldous)


    Hello I need a code for average rating for example I have a page with 20 posts and I want to show every average on thier post so that user can see which post have most rating and by the way I don’t want a shortcode but I want a code so I can use it in my template

Viewing 1 replies (of 1 total)
  • Plugin Contributor Kamal Khan

    (@bhittani)

    
    <?php
    $best = (int) get_option('kksr_stars', 5);
    $score = (int) get_post_meta($postId, '_kksr_ratings', true);
    $votes = (int) get_post_meta($postId, '_kksr_casts', true);
    
    $avg = $score && $votes ? round((float)(($score/$votes)*($best/5)), 1) : 0;
    $per = $score && $votes ? round((float)((($score/$votes)/5)*100), 2) : 0;
    
    // $avg contains the average
    // $per contains the percentage
    ?>
    

    $postId is the id of the (current) post.

    • This reply was modified 6 years, 3 months ago by Kamal Khan. Reason: Optimize
Viewing 1 replies (of 1 total)
  • The topic ‘Code for average rating’ is closed to new replies.