Skip to content

Instantly share code, notes, and snippets.

@nvourva
Created June 4, 2019 13:32
Show Gist options
  • Save nvourva/91212b6c07f59e45f40146dd8ff84054 to your computer and use it in GitHub Desktop.
Save nvourva/91212b6c07f59e45f40146dd8ff84054 to your computer and use it in GitHub Desktop.
Save average rating on post meta for ci-comment-rating
//Save the rating submitted by the user.
add_action( 'comment_post', 'ci_comment_rating_save_comment_rating' );
function ci_comment_rating_save_comment_rating( $comment_id ) {
if ( ( isset( $_POST['rating'] ) ) && ( '' !== $_POST['rating'] ) ) {
$rating = intval( $_POST['rating'] );
add_comment_meta( $comment_id, 'rating', $rating );
$postid = get_comment( $comment_id )->comment_post_ID;
if ( metadata_exists('post', $postid, 'post_average_rating') ) {
update_post_meta( $postid, 'post_average_rating', ci_comment_rating_get_average_ratings( $postid ) );
} else {
add_post_meta( $postid, 'post_average_rating', ci_comment_rating_get_average_ratings( $postid ), true );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment