OhhMuaOhhMua
  • Home
  • Hosting
    • Best Web Hosting
    • Free Web Hosting
    • VPS Hosting
  • WordPress
    • Solutions & Troubleshooting
    • Installation & Setup
    • Themes & Plugins
    • Security
  • Computer Tips
    • Gaming Errors & Solutions
    • PC Troubleshooting
    • Computer Hardware
  • Coupons & Deals
  • Contact Us
Reading: How to Increase the Star Ratings for KK Star Ratings
Share
Notification Show More
Font ResizerAa
OhhMuaOhhMua
Font ResizerAa
  • Computer Tips
  • Hosting
  • WordPress
Search
  • Home
  • WordPress
    • Installation & Setup
    • Security
    • Solutions & Troubleshooting
    • Themes & Plugins
    • Troubleshooting
  • Hosting
    • Free Web Hosting
    • VPS Hosting
    • Best Web Hosting
  • Computer Tips
    • Computer Hardware
  • Coupons & Deals
Have an existing account? Sign In
Follow US
Copyright © 2024 ohhmua. All rights reserved.
OhhMua > Blog > WordPress > Solutions & Troubleshooting > How to Increase the Star Ratings for KK Star Ratings
Solutions & Troubleshooting

How to Increase the Star Ratings for KK Star Ratings

ohhmua
Last updated: September 8, 2024 4:05 pm
ohhmua
Share
16 Min Read
How to Increase the Star Ratings for KK Star Ratings 1
How to Increase the Star Ratings for KK Star Ratings 1
SHARE
Contents
Code to Increase Star Ratings to 4>5 StarsCSS to Block Low Star RatingsCode to Increase Star Ratings and Add Ratings for Posts Without Ratings

KK Star Ratings is very familiar to WordPress users; it helps add star ratings to posts and displays beautiful star ratings in Google search results.

It wouldn’t be an issue if the ratings were genuine, but in some cases, competitors might try to sabotage by giving all 1-star ratings. As a result, the display on Google looks bad, significantly affecting search results and user perception when they see a post with only 1-star ratings.

How to Increase the Star Ratings for KK Star Ratings
How to Increase the Star Ratings for KK Star Ratings

In this article, I will share with you a code snippet that automatically converts ratings below 4 stars to 4–5 stars. The goal is not to make everything 5 stars but to set the ratings between 4 and 5 to make them look more authentic.

How to Increase the Star Ratings for KK Star Ratings 1
How to Increase the Star Ratings for KK Star Ratings

Code to Increase Star Ratings to 4>5 Stars

Add the following code to the functions.php file located in wp-content/themes/[your theme]/functions.php.

/*
 * Set star ratings to 4>5 stars for KK Star Rating
 * Author: ohhmua.com
 */
add_action( 'wp_ajax_update_kk_star_rating', 'devvn_update_kk_star_rating_func' );
function devvn_update_kk_star_rating_func() {
    global $wpdb;
    $posts_with_low_avg = $wpdb->get_results("
        SELECT post_id, meta_value as avg_rating 
        FROM $wpdb->postmeta 
        WHERE meta_key = '_kksr_avg'
        AND CAST(meta_value AS DECIMAL(3,2)) < 4
    ");
    $posts_with_low_avg_table = array();
    if (!empty($posts_with_low_avg)) {
        foreach ($posts_with_low_avg as $post) {
            $post_id = $post->post_id;
            $count = (int) get_post_meta($post_id, '_kksr_count_default', true);
            $old_avg = (float) get_post_meta($post_id, '_kksr_avg_default', true);
            $random_rating = mt_rand(40, 50) / 10;
            $new_ratings = $count * $random_rating;
            $new_avg = $new_ratings / $count;
            update_post_meta($post_id, '_kksr_ratings_default', $new_ratings);
            update_post_meta($post_id, '_kksr_ratings', $new_ratings);
            update_post_meta($post_id, '_kksr_count_default', $count);
            update_post_meta($post_id, '_kksr_casts', $count);
            update_post_meta($post_id, '_kksr_avg_default', $new_avg);
            update_post_meta($post_id, '_kksr_avg', $new_avg);
            $posts_with_low_avg_table[] = compact('post_id', 'old_avg', 'new_avg');
        }
        $mess = '<span style="color: red;">Rating has been updated.</span>';
    } else {
        $mess = '<span style="color: green;">No posts need updating.</span>';
    }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Manually Set Ratings for KK Star Rating</title>
        <style>
            * {
                margin: 0;
                padding: 0;
                border: 0;
                font-size: 100%;
                font: inherit;
                vertical-align: baseline;
                line-height: 1;
                box-sizing: border-box;
                -moz-box-sizing: border-box;
                -webkit-box-sizing: border-box;
            }
            body {
                font-size: 14px;
            }
            .container{
                padding: 20px;
                max-width: 900px;
                width: 100%;
                margin: 0 auto;
            }
            table {
                width: 100%;
                border: 1px solid #ddd;
                border-collapse: collapse;
                border-spacing: 0;
            }
            table td, table th {
                padding: 5px;
                text-align: center;
                border: 1px solid #ddd;
            }
            h1 {
                font-size: 20px;
                text-align: center;
                margin-bottom: 20px;
            }
        </style>
    </head>
    <body>
    <div class="container">
        <h1><?php echo $mess;?></h1>
        <?php
        if($posts_with_low_avg_table){
            ?>
            <table>
                <thead>
                <tr>
                    <td>ID</td>
                    <td>Post Title</td>
                    <td>Old Rating</td>
                    <td>New Rating</td>
                    <td>Link to Post</td>
                </tr>
                </thead>
                <tbody>
                <?php
                foreach ($posts_with_low_avg_table as $item){
                    extract($item);
                    ?>
                    <tr>
                        <td><?php echo $post_id;?></td>
                        <td><?php echo get_the_title($post_id);?></td>
                        <td><?php echo $old_avg;?></td>
                        <td><?php echo $new_avg;?></td>
                        <td><a href="<?php echo get_the_permalink($post_id);?>" title="View Post" target="_blank">View Post</a></td>
                    </tr>
                    <?php
                }
                ?>
                </tbody>
            </table>
            <?php
        }
        ?>
    </div>
    </body>
    </html>
    <?php
    die();
}

After that, open your browser and access the following URL while logged into your website. This means you must be logged in as an admin for this URL to work.

  1. https://[your-domain]/wp-admin/admin-ajax.php?action=update_kk_star_rating

For example, if my site is ohhmua.com, I would go to:
https://ohhmua.com/wp-admin/admin-ajax.php?action=update_kk_star_rating

CSS to Block Low Star Ratings

Additionally, if you have new posts where you don’t want users to give 1, 2, or 3-star ratings but only allow 4 or 5 stars, you can add the following CSS to your website. If you’re not familiar with coding, you can add it by going to the menu: Appearance > Customize > Additional CSS.

.kk-star-ratings .kksr-stars > div > div:nth-child(1),
.kk-star-ratings .kksr-stars > div > div:nth-child(2),
.kk-star-ratings .kksr-stars > div > div:nth-child(3) {
    pointer-events: none;
}

Code to Increase Star Ratings and Add Ratings for Posts Without Ratings

This code is more advanced. The code above only helps increase existing ratings. However, the code below will both increase and add ratings for posts that do not have star ratings.

If you’re using this code, remove the previous one and replace it with this code.

/*
 * Set star ratings to 4>5 stars for kk star rating
 * This code also adds ratings to posts that haven't been rated yet
 * Author: ohhmua.com
 */
add_action( 'wp_ajax_update_kk_star_rating', 'devvn_update_kk_star_rating_func' );
function devvn_update_kk_star_rating_func() {
    global $wpdb;
    $posts_with_low_avg = $wpdb->get_results("
        SELECT p.ID as post_id, pm.meta_value as avg_rating
        FROM $wpdb->posts p
        LEFT JOIN $wpdb->postmeta pm
        ON p.ID = pm.post_id 
        AND pm.meta_key = '_kksr_avg'
        WHERE (pm.meta_value IS NULL OR CAST(pm.meta_value AS DECIMAL(3,2)) < 4)
        AND p.post_type IN ('post')
        AND p.post_status = 'publish'
    ");
    $posts_with_low_avg_table = array();
    if (!empty($posts_with_low_avg)) {
        foreach ($posts_with_low_avg as $post) {
            $post_id = $post->post_id;
            $count = (int) get_post_meta($post_id, '_kksr_count_default', true);
            $old_avg = (float) get_post_meta($post_id, '_kksr_avg_default', true);
            if(!$count) $count = rand(10,50); //This part randomizes the total number of ratings if there aren't any. You can change this to a higher number.
            $random_rating = mt_rand(40, 50) / 10; //This part defines the desired star rating. For example, to set between 4.5 and 5 stars, change to mt_rand(45, 50) / 10
            $new_ratings = $count * $random_rating;
            $new_avg = $new_ratings / $count;
            update_post_meta($post_id, '_kksr_ratings_default', $new_ratings);
            update_post_meta($post_id, '_kksr_ratings', $new_ratings);
            update_post_meta($post_id, '_kksr_count_default', $count);
            update_post_meta($post_id, '_kksr_casts', $count);
            update_post_meta($post_id, '_kksr_avg_default', $new_avg);
            update_post_meta($post_id, '_kksr_avg', $new_avg);
            $posts_with_low_avg_table[] = compact('post_id', 'old_avg', 'new_avg');
        }
        $mess = '<span style="color: red;">Ratings updated.</span>';
    } else {
        $mess = '<span style="color: green;">No posts need updating.</span>';
    }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Manual Star Rating Update for KK Star Rating</title>
        <style>
            * {
                margin: 0;
                padding: 0;
                border: 0;
                font-size: 100%;
                font: inherit;
                vertical-align: baseline;
                line-height: 1;
                box-sizing: border-box;
                -moz-box-sizing: border-box;
                -webkit-box-sizing: border-box;
            }
            body {
                font-size: 14px;
            }
            .container{
                padding: 20px;
                max-width: 900px;
                width: 100%;
                margin: 0 auto;
            }
            table {
                width: 100%;
                border: 1px solid #ddd;
                border-collapse: collapse;
                border-spacing: 0;
            }
            table td, table th {
                padding: 5px;
                text-align: center;
                border: 1px solid #ddd;
            }
            h1 {
                font-size: 20px;
                text-align: center;
                margin-bottom: 20px;
            }
        </style>
    </head>
    <body>
    <div class="container">
        <h1><?php echo $mess;?></h1>
        <?php
        if($posts_with_low_avg_table){
            ?>
            <table>
                <thead>
                <tr>
                    <td>ID</td>
                    <td>Post Title</td>
                    <td>Old Rating</td>
                    <td>New Rating</td>
                    <td>Link to Post</td>
                </tr>
                </thead>
                <tbody>
                <?php
                foreach ($posts_with_low_avg_table as $item){
                    extract($item);
                    ?>
                    <tr>
                        <td><?php echo $post_id;?></td>
                        <td><?php echo get_the_title($post_id);?></td>
                        <td><?php echo $old_avg;?></td>
                        <td><?php echo $new_avg;?></td>
                        <td><a href="<?php echo get_the_permalink($post_id);?>" title="View Post" target="_blank">View Post</a> </td>
                    </tr>
                    <?php
                }
                ?>
                </tbody>
            </table>
            <?php
        }
        ?>
    </div>
    </body>
    </html>
    <?php
    die();
}

In the code above, the line AND p.post_type IN (‘post’) applies only to posts. If you want to include other post types, such as ‘page’, change that line to AND p.post_type IN (‘post’, ‘page’).

That’s it!

Note:

  • You can delete the code after it has run.
  • Alternatively, you can leave it and run it occasionally.
  • If there are too many posts to update, the process may be slow. Please wait until it finishes, then refresh the page until you see the message indicating there are no more posts to update.

You Might Also Like

How to Add a “Read More” and “Show Less” Button to Product Descriptions in WooCommerce

Blog, E-Commerce, or Forum? WordPress Covers Every Website Type!

How I Optimized Pagination and Skyrocketed Traffic

How to Pick the Ideal Blogging Platform for Your Needs

Protecting Your WordPress Site from File Upload Vulnerabilities

TAGGED:KK Star Ratings
Share This Article
Facebook Twitter Email Print
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Trending Stories

5 Signs It's Time to Upgrade Your Computer 5
Computer Tips

5 Signs It’s Time to Upgrade Your Computer

July 24, 2023
How to Pick the Ideal Blogging Platform for Your Needs
Solutions & Troubleshooting

How to Pick the Ideal Blogging Platform for Your Needs

April 1, 2025
Title and Meta Description Mismatch on Google
Solutions & Troubleshooting

Title and Meta Description Mismatch on Google[How to fix]

September 25, 2024
How to Check Computer Power Supply
Computer Hardware

How to Check Computer Power Supply

July 29, 2023
WordPress Covers Every Website Type
Solutions & Troubleshooting

Blog, E-Commerce, or Forum? WordPress Covers Every Website Type!

April 29, 2025
admin wordpress dashboard
Themes & Plugins

Introduction to the WordPress Dashboard: Features, Tools, and Navigation

April 25, 2025

You Might Also Like

Contact button
Solutions & Troubleshooting

Contact button in the footer with added call button shake effect

September 21, 2024
Guide to Creating a Beautiful Promotion Notification Box for Your Website
Solutions & Troubleshooting

How to Create a Stunning Notification Box for Your Website

September 20, 2024
Create a Simple Incrementing Number Effect with HTML and JavaScript 2
Solutions & Troubleshooting

Create a Simple Incrementing Number Effect with HTML and JavaScript

September 17, 2024
Guide to Using Multiple Domains for One WordPress Website
Solutions & Troubleshooting

Guide to Using Multiple Domains for One WordPress Website

September 16, 2024
Previous Next
newsletter featured

Always Stay Up to Date

Subscribe to our newsletter to get our newest articles instantly!

Follow US on Social Media

Facebook Youtube Steam Twitch Unity

Copyright © 2024 ohhmua. All rights reserved.

OhhMua

Information

  • About
  • Terms & Conditions
  • Privacy Policy
Welcome Back!

Sign in to your account

Lost your password?