0

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.

7
  • You are obviously missing the part that would actually "perform some operations" but other than that, what isn't working? Also, 0%10 === 0 so this will probably execute when you don't want it to.
    – s_ha_dum
    Commented Jun 30, 2013 at 19:35
  • Thank you @s_ha_dum. I edited adding the operations, for the sake of simplicity I didnt add them. and about 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? Commented Jun 30, 2013 at 19:37
  • 1
    Yes, but what you have will execute when $count equals 0 as well as 10,20,30 and so on.
    – s_ha_dum
    Commented Jun 30, 2013 at 19:39
  • I agree with you, but when I execute the sql query in phpmyadmin it gives me a count different than zero, and it is incremented correcly when a comment meta is changed from 0 to 1 Commented Jun 30, 2013 at 19:41
  • Okey-dokey. It is your (potential) bug, not mine. You didn't answer my question, "What isn't working?" It is hard to tell you what is missing if I don't know what is wrong.
    – s_ha_dum
    Commented Jun 30, 2013 at 19:44

1 Answer 1

1

I think you only want get_comment and not get_comments on this line:

$com=get_comments($comment_id);

You are only checking for a single comment, by the ID. get_comments won't do that the way you are using it.

There is a check halfway through the update_metadata function that compares the existing value to new value under some circumstances. That runs before the update_comment_meta hook and sometimes returns so that that hook never runs. Admittedly this is a guess, but if you are passing the same value, such as true every time this may be where it hangs.

The update_{$meta_type}_metadata hook would fire, but is a filter and not really the best choice for this kind of hook. I am wondering if you shouldn't be using the edit_comment hook instead.

3
  • I am really sorry for not giving edit about your first remark, I changed get_comments by get_comment, and also I changed the SQL with the code you provided me in one of your answers. Give me one minute and I will provide with the whole new code. Commented Jul 4, 2013 at 14:39
  • I edited and I am now trying to understand the other remarks and ideas you provided, really big thanks for the help you are always giving me and giving to the whole wordpress developpers. Commented Jul 4, 2013 at 14:43
  • Go back to chat please. Use the link in the last comment under the question
    – s_ha_dum
    Commented Jul 4, 2013 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.