I would like to save some PHP memory when quering category page (like 5000 products at once) with average review rating, so I would like to save average rating of reviews every time I will manually approve new comment.
I would like to accomplish, whenever I click on "APPROVE" button in administration, it will run these php scripts.
function average_rating() {
global $wpdb;
$post_id = get_the_ID();
$ratings = $wpdb->get_results("
SELECT $wpdb->commentmeta.meta_value
FROM $wpdb->commentmeta
INNER JOIN $wpdb->comments on $wpdb->commentsment_id=$wpdb->commentmetament_id
WHERE $wpdb->commentmeta.meta_key='rating'
AND $wpdb->commentsment_post_id=$post_id
AND $wpdb->commentsment_approved = 1
and trim(coalesce($wpdb->commentmeta.meta_value, '')) <>''
");
$counter = 0;
$average_rating = 0;
if ($ratings) {
foreach ($ratings as $rating) {
$average_rating = $average_rating + $rating->meta_value;
$counter++;
}
//round the average to the nearast 1/2 point
return (round(($average_rating/$counter)*2,0)/2);
} else {
//no ratings
return '0';
}
}
$ar = average_rating();
add_post_meta( $post->ID, 'summary', $ar );
I would like to save some PHP memory when quering category page (like 5000 products at once) with average review rating, so I would like to save average rating of reviews every time I will manually approve new comment.
I would like to accomplish, whenever I click on "APPROVE" button in administration, it will run these php scripts.
function average_rating() {
global $wpdb;
$post_id = get_the_ID();
$ratings = $wpdb->get_results("
SELECT $wpdb->commentmeta.meta_value
FROM $wpdb->commentmeta
INNER JOIN $wpdb->comments on $wpdb->commentsment_id=$wpdb->commentmetament_id
WHERE $wpdb->commentmeta.meta_key='rating'
AND $wpdb->commentsment_post_id=$post_id
AND $wpdb->commentsment_approved = 1
and trim(coalesce($wpdb->commentmeta.meta_value, '')) <>''
");
$counter = 0;
$average_rating = 0;
if ($ratings) {
foreach ($ratings as $rating) {
$average_rating = $average_rating + $rating->meta_value;
$counter++;
}
//round the average to the nearast 1/2 point
return (round(($average_rating/$counter)*2,0)/2);
} else {
//no ratings
return '0';
}
}
$ar = average_rating();
add_post_meta( $post->ID, 'summary', $ar );
Share
Improve this question
asked Jul 4, 2016 at 12:01
Milan NovakMilan Novak
1
1 Answer
Reset to default 1Sadly there are no hooks or filters that you could use in edit-comments.php
. You'd have to build your own admin page with a custom button and use wp_set_comment_status( $comment, 'approve' );
to approve comment and do whatever you need to do...
EDIT:
I was wrong - there's the wp_set_comment_status
action inside wp_set_comment_status
function https://wpseek/function/wp_set_comment_status/
do_action( 'wp_set_comment_status', $comment->comment_ID, $comment_status );
So you can just do:
add_action('wp_set_comment_status','average_rating',10,2);
and add to your function
function average_rating($comment_id, $comment_status) {
if ($comment_status == 'approve') {
.....rest of your code