Customize kk Star Rating to get rating from different custom field
-
Hi,
This is somewhat related to the thread here: https://www.ads-software.com/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.
https://www.ads-software.com/extend/plugins/kk-star-ratings/
- The topic ‘Customize kk Star Rating to get rating from different custom field’ is closed to new replies.