WordPress: count all user comments and then count metadata by comments

I set some regular metam against specific comments for users using this function: add_comment_meta( $wp_comment_id, 'accepted', true );

What I want to do is to show each user their profile / author / username, how many of these special comments they have, for example, if the user made 20 comments and 5 had this metadata accepted equal to true, then the value will be 5 .

How can i do this? Thank.

+3
source share
2 answers

I assume this is the latest version of wordpress. Here you can see the schema of the database schema: http://codex.wordpress.org/images/9/9e/WP3.0-ERD.png

, - :

<?php
getCommentCount('John', 'accepted', 'true');

function getCommentCount($author_name, $meta_key, $meta_value){
    if(empty($author_name)){
        return;
    }

    $author_name    = trim($author_name);
    $sql            = 'SELECT count(*) FROM ' . $wpdb->comments . ' comments '
                    . ' INNER JOIN ' . $wpdb->commentmeta . ' meta ON comments.comment_ID = meta.comment_id '
                    . ' WHERE comments.comment_author = %s AND meta.meta_key = %s ANd meta.value = %s ';

    $commentCount   = $wpdb->get_var($wpdb->prepare($sql, $author_name, $meta_key, $meta_value));

    return $commentCount;
}
+3

, " "...
whice . .

- , ... php

function countUserComments($userID) {
    $args = array(
        'user_id' => $userID
    );
    $comments = get_comments( $args );
    echo count($comments);
}

,

<?php echo countUserComments($user->ID); ?>

.

, ;) Cheers, Sagive

+1

Source: https://habr.com/ru/post/1792009/


All Articles