kk Star Ratings
Default rating only at first publish (1 post)

  1. skursvamp
    Posted 3 years ago #


    First of all I would like to thank you for this awesome plugin.

    I however have a small problem with setting default rating on my posts. I found your solution from another support ticket on how to set a default rating when publishing a post.

    This is the code I talk about:

    add_action('save_post', 'auto_rate_post');
    function auto_rate_post($post_id)
    	if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
    		return $post_id;
    	if(!isset($_POST['post_type']) || !current_user_can('edit_post', $post_id))
    		return $post_id;
    	$stars = 4;
    	$total_stars = is_numeric(get_option('kksr_stars')) ? get_option('kksr_stars') : 5;
    	if(!get_post_meta($post_id, '_kksr_ratings', true))
    		$ratings = $stars / ($total_stars/5);
    		$avg = $ratings ? number_format((float)$ratings, 2, '.', '') : 0;
    		update_post_meta($post_id, '_kksr_ratings', $ratings);
    		update_post_meta($post_id, '_kksr_casts', 1);
    		update_post_meta($post_id, '_kksr_avg', $avg);
    	return $post_id;

    This code works perfectly when I publish a post for the first time. My problem is that when I update a post the current rating is getting overwritten by the rating (4) in the example code above.

    I also have a few posts with rating 0 that are saved as draft for now. I want them to get the rating "3" when I publish them.

    So I only want to put this default rating on new posts or on posts that have rating = 0. In other cases I want to keep the current rating.

    I canĀ“t seem to figure out how to modify your code example to fix this.

    Thanks in advance for your help!


Topic Closed

This topic has been closed to new replies.

About this Plugin

  • kk Star Ratings
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic