DBL DeathDealer
asked 5 years ago

I am looking to see if there is an easy method to add the number of comments for a question asked on the main display for questions and answers.

https://www.dropbox.com/s/lhgc884brlrp43a/comments-area.png?dl=0

I want users to comment as well as answer questions. We are using the plugin for a discussion board and Q&A board.

Thanks.

3 Answers
Allen
answered 5 years ago

@DBL DeathDealer : hi i think i can help you get it. Here, follow my instruction then :

First of all , you need to add a new section for comment count in the main page by open this file : dw-question-answer\inc\templates\default\content-question, end then go to line 20 , below there , add these code :

<div class="dwqa-comment">
    <?php $answer_comments = dwqa_question_comment_count(); ?>
    <?php if ( $answer_comments > 0 ) {
        printf(
            '<strong>%d</strong> %s',
            $answer_comments,
            _n( 'comment', 'comments', $answer_comments, 'dwqa' )
        ); ?>
    <?php } else {
        echo '<strong>0</strong> '.__( 'comment','dwqa' );
        }
    ?>
    </div>

and then , to make it work , you can go to file : dw-question-answer\inc\actions.php and add this code below :

function dwqa_question_comment_count( $question_id = null ) {
    global $wpdb;
    if ( ! $question_id ) {
        global $post;
        $question_id = $post->ID;
    }
    $comment_count = get_transient( 'dwqa_comment_count_for_' . $question_id );

    if ( false === $comment_count ) {
        $question_sql = "SELECT COUNT( DISTINCT `C`.comment_ID ) FROM {$wpdb->comments} C WHERE `C`.comment_post_ID = {$question_id}";

        $question_comment =  $wpdb->get_var( $question_sql );

        $answer_sql = "SELECT `P`.ID FROM {$wpdb->posts} P JOIN {$wpdb->postmeta} PM ON `PM`.post_id = `P`.ID WHERE `PM`.meta_key = '_question' AND meta_value = {$question_id} AND `P`.post_type = 'dwqa-answer' AND `P`.post_status = 'publish'";

        $answer_id = $wpdb->get_results( $answer_sql );

        $answer_comment = 0;

        if ( $answer_id && !empty( $answer_id )  ) {
                foreach ($answer_id as $answer ) {
                    $single_answer_comment_sql = "SELECT COUNT( DISTINCT `C`.comment_ID ) FROM {$wpdb->comments} C WHERE `C`.comment_post_ID = {$answer->ID}";
                    $single_answer_comment = $wpdb->get_var( $single_answer_comment_sql );
                    $answer_comment += $single_answer_comment;
                }
            }   

        $comment_count = $question_comment + $answer_comment;

        set_transient( 'dwqa_comment_count_for_' . $question_id, $comment_count, 60*60*6 );

    }

    return $comment_count;

}

That function will help you get the comments of a question, so you can place it at the bottom or any where you want. Hope this help. Glad

DBL DeathDealer
answered 5 years ago

@nobita

Code seems to be working perfect, except minor snafu as it is not counting new comments that are posted.

So, published code both locations, it showed proper numbers. New Comment added to discussion, the number did not change properly. Any thoughts?

I will dig a bit to see if anything in the code causing the error.

nobita
replied 5 years ago

@DBL DeathDealer : after re-scan , i think it maybe problem with :
$comment_count = get_transient( 'dwqa_comment_count_for_' . $question_id );

and set_transient( 'dwqa_comment_count_for_' . $question_id, $comment_count, 60*60*6 );..

Maybe i was wrong about it. You could give it a try by remove it and remove the condition : false === $comment_count . Hope this help.

nobita
replied 5 years ago

i set the transient to 1 day expire so, the next day , the comment will be update … it’s kinda be like that i think …

DBL DeathDealer
answered 5 years ago

Thanks @nobita

Powered by DW Question & Answer Pro