0

I have a voting function for comments which needs to display the avatars of the users who voted for a specific comment. I am having trouble getting the user id of the voter ($supporter). All I seem to get is the user ID of the user who made the comment > I need to get the user ID of the user who voted.

My function:

function addServiceSupporter($comment_id, $user_id) {
    add_comment_meta( $comment_id, 'supporting_user', $user_id, false );
}

So the $user_id is stored under meta_value in the wp_commentmeta table.

Before this function I have this the following which seems to have a user_id in it.

/**
 * After an ajax call try to add the supporter
 */
if (isset($_POST['new_service'])) {
    $new_service_supporter = $_POST['new_service'];
    try {
        addServiceSupporter($new_service_supporter['comment_id'], $new_service_supporter['user_id']);
    } catch (Exception $e) {
        print_r($e);
    }
    die;
}

The call:

foreach($supporters as $supporter) {
    $supporting_user = get_comment_meta(get_comment_ID(),'supporting_user', true);
    $avatar = get_avatar($supporting_user, 24 );

    echo $avatar; 
}

I also have tried:

$avatar = get_avatar($supporting_user, 24 );

But that of course gives the avatar of the user currently active on the page.

How do I get the $user_id aka meta_value of this comment_meta?

The result should be: Comment votes (or likes if you want) by users to the comments of others. Then showing the avatars of the people who voted. My problem = how to get the user id of the voters (not the comment author, not the current user) so I can show the right avatar?

  • what is the code before the line foreach($supporters as $supporter) { ? – Shazzad Feb 24 '14 at 17:17
  • that would be global $current_user; get_currentuserinfo(); – inTOWN Feb 24 '14 at 17:27
0

You are saving your data with non-unique key (false as last argument), but retrieve it it as singular data (true as last argument).

This is likely returning you the first voter on comment. You need to retrieve complete array of values and work with it to be operating on complete data.

  • Would you know what I should put instead of true? Leaving it empty, or false, or anything else gives met an "Array to string conversion" – inTOWN Feb 24 '14 at 17:27
  • @inTOWN yes, that's the point, without true you get array of all values and need to process it to get what you actually want (last value for example) – Rarst Feb 24 '14 at 17:28
  • Could you please give me an example of who I could solve this? (sorry, this part is not my strongest point) – inTOWN Feb 24 '14 at 17:31
  • @inTOWN still fuzzy on what you are actually trying to do and this is more in realm of basic PHP. Try to update your question with more information. – Rarst Feb 24 '14 at 17:35
  • I just updated the question. The problem is that I need to get the ID of the voter so I can show the right avatar. Right now I only seem to be capable of getting the ID of the a) Comment author or b) the current user. – inTOWN Feb 24 '14 at 17:38
0

What I had to do was to:

$supporters = get_comment_meta(get_comment_ID(),'supporting_user', false);
  1. Rename $supporting_user to $supporter (just like the foreach)
  2. change true into false

Full code:

$supporters = get_comment_meta(get_comment_ID(),'supporting_user', false);
foreach($supporters as $supporter) {
$avatar = get_avatar($supporter, 24 );
echo $avatar; 
}

Thanks @Rast for trying to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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