By analogy to the hook on post meta update, I use the following code for hooking when comment meta value is updated.
My comment meta key is "consider" and can have 2 values: 0 and 1.
My Goal is to count number of comments with consider=1
, and when this count reaches 10, perform some operations.
add_action( 'update_comment_meta', 'ten_considered_comments_badge');
function ten_considered_comments_badge($meta_id, $comment_id, $key, $value)
{
if ( 'consider' != $key or 1!= $value) return;
$com=get_comment($comment_id);
if ( !$com ) return;
$args = array(
'type' => '',
'user_id' => $com->user_id,
'meta_key' => 'consider',
'meta_value' => '1',
);
$com2= get_comments($args);
$count = count($com2);
if( $count%10==0) {
$number_of_bronze_badges= get_user_meta($com->user_id,'user_number_of_bronze_badges',true);
$number_of_bronze_badges= $number_of_bronze_badges + 1;
update_user_meta($com->user_id, 'user_number_of_bronze_badges', $number_of_bronze_badges);
}
}
What I am missing in this code . Your help is highly appreciated.
0%10 === 0
so this will probably execute when you don't want it to.if( $count%10==0)
, it is because i want the operations be done when $count is equal to 10, 20, 30...At first, is the logic behind the hook correct?$count
equals 0 as well as 10,20,30 and so on.