• Resolved razorharrison

    (@razorharrison)


    Hi There,

    I’ve been messing around with this code for ages and don’t seem any closer then I was before I started.

    Essentially, within the comments.php loop, I am attempting to calculate the total number of posts and comments made by each user who are listed in each thread. I am not looking to calculate the number of comments in the thread, but rather the total number of comments and new posts created in total. So for example, I’d like to be able to say

    User : Foo
    Total Posts : =sum(all posts and comments ever made).
    On : https://www.reviewrecruiter.com.au/members/forum-chat?view=blogs
    So – on the comments section, I’d like to have Total Posts : (comments + posts)

    The process I’ve been following is to grab the comment_author() but this returns null if the user has not entered in their full name. In my case, I am not allowing users to enter firstname and surname as part of the registration process, rather just their logon name. However, this returns anonymous if the user has not filled in these details.

    Even when using the get_user_by_email() it requires the login name which again I can’t seem to locate.

    Does anyone have any ideas or suggestions on how to do this?

Viewing 1 replies (of 1 total)
  • Thread Starter razorharrison

    (@razorharrison)

    Ok – in accordance with the precedent thus far of communicating with myself, I have managed to solve the issue.

    In this theme I am using Arjuna-x and I am very confident that for some reason this seems to mess up the comments.

    As such, the only solution in the comments.php file was the extract the user_id via e-mail and use that as the identifier to get the info I needed.
    The solution follows – for those who may be interested;

    <?php $user2get = $comment->comment_author_email ; ?>
    <?php //echo $user2get; ?>
    <?php $userid = get_user_by_email($user2get); ?>
    <?php $thenewid = $userid->ID; ?>
    <?php //echo $thenewid; ?>

    <?php
    global $wpdb;
    $user_id = $thenewid; //change this if not in a std post loop
    $where = ‘WHERE comment_approved = 1 AND user_id = ‘ . $user_id ;
    $comment_count = $wpdb->get_var(
    “SELECT COUNT( * ) AS total
    FROM {$wpdb->comments}
    {$where}
    “);
    $user = get_userdata($user_id);
    $post_count = get_usernumposts($user->ID);

    ?>

    <?php echo $comment_count + $post_count; ?>

    Love to all !

Viewing 1 replies (of 1 total)
  • The topic ‘Calculating number of user comments and posts’ is closed to new replies.