WordPress.org

Ready to get started?Download WordPress

Forums

kk Star Ratings
[resolved] Customize kk Star Rating to get rating from different custom field (4 posts)

  1. bekar09
    Member
    Posted 1 year ago #

    Hi,

    This is somewhat related to the thread here: http://wordpress.org/support/topic/setting-default-rating-for-kk-stars. I didn't want to thread jack that topic hence posting a new one.

    I have two different custom fields: book_rating (float) and book_votes (count) which are pre-populated for all posts(posted automatically as drafts). I have changed all instances of '_kksr_avg' and '_kksr_casts' from the plugin files. This I know is not a good thing o do because when I upgrade the plugin, I will love my changes.

    Now my question is when I publish a particular post I want to set '_kksr_ratings' automatically. For this I have the below code:

    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;
    	$stars = get_field('field_519ad9c6f5cf5'); // This value is the average e.g. 6.3
    
    	$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);
    		$vote_count = get_field('field_519ada96f5cf6');  // Plugin ACF used
    		$avg = $ratings ? number_format((float)$ratings, 2, '.', '') : 0;
    
    		update_post_meta($post_id, '_kksr_ratings', $ratings);
    		update_field( 'field_519ada96f5cf6', $vote_count, $post_id );  // Plugin ACF used
    		update_field( 'field_519ad9c6f5cf5', $avg, $post_id );  // Plugin ACF used
    
    	}
    
    	return $post_id;
    }
    
    add_action('save_post', 'auto_rate_post');

    The count of casts is getting set properly, the '_kksr_ratings' also seem to be set properly, but the stars do not appear.

    I would really appreciate if somebody has done something similar and would share the right way to do this.

    http://wordpress.org/extend/plugins/kk-star-ratings/

  2. Kamal Khan
    Member
    Plugin Author

    Posted 1 year ago #

    $stars should be an integer. It is the amount of stars a user rates so can only be an integer.

  3. bekar09
    Member
    Posted 1 year ago #

    Got it. Well I reverted back changes in your plugin file and decided to store values in the default plugin custom fields. I also modified my code as below:

    $avg = get_field('field_519ad9c6f5cf5'); // Returns 6.3
    	$votes_cast = get_field('field_519ada96f5cf6'); // Returns 1900
    	$stars = ceil($avg);
    
    	$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); // $votes_cast
    		update_post_meta($post_id, '_kksr_avg', $avg);
    	}

    This works fine and outputs "6.00/10 (60.00%) 1 vote", but if I replace update_post_meta($post_id, '_kksr_casts', $votes_cast);
    it does not work anymore.

    It shows "0.00/10 (0.03%) 1900 votes", but the stars are not fuelled. What am I doing wrong?

    Thanks.

  4. bekar09
    Member
    Posted 1 year ago #

    Hi Kamal,

    Sorry for being so stupid. I fixed the problem. Pasting the snippet below so that it can be of help to others if required.

    $avg = get_field('field_519ad9c6f5cf5');
    	$avg = number_format((float)($avg/2), 2, '.', '');
    	$votes_cast = get_field('field_519ada96f5cf6');
    
    	// $total_stars = is_numeric(get_option('kksr_stars')) ? get_option('kksr_stars') : 5;
    
    	if(!get_post_meta($post_id, '_kksr_ratings', true))
    	{
    		$ratings = $votes_cast * $avg;
    		// $avg = $ratings ? number_format((float)$ratings, 2, '.', '') : 0;
    
    		update_post_meta($post_id, '_kksr_ratings', round($ratings));
    		update_post_meta($post_id, '_kksr_casts', $votes_cast);
    		update_post_meta($post_id, '_kksr_avg', $avg);
    	}

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic