Rohit D
asked 4 years ago

Hi. Is there a way in DW Answers to show total count of questions asked by a user and total count of answers of a user on his buddypress profile?

2 Answers
Allen
answered 4 years ago

Hi, for this feature , i think you need to add custom code to it , You need 2 functions , 1 for question count and 1 for answer count. You can add these code to the function file , and when using this 2 function , you can get the question and answer of the current login user.

if ( is_user_logged_in() ) {
    global $bp;
    $user_id = $bp->displayed_user->id;
    function get_question_count( $user_id ){
        global $wpdb;
        $query = 'SELECT COUNT(DISTINCT p.id) AS question_count FROM '.$wpdb->prefix.'posts p WHERE p.post_type="dwqa-question" AND p.post_status="publish" AND p.post_author='.$user_id;
        $result = $wpdb->get_results( $query );
        $question_count = $result[0]->question_count;
        return $question_count;
    }

    function get_answer_count( $user_id ) {
        global $wpdb;
        $query = 'select COUNT(DISTINCT p.id) as answer_count from '.$wpdb->prefix.'posts p join '.$wpdb->prefix.'postmeta p1 on p.ID = p1.post_id where p.post_status = "publish" and p.post_type = "dwqa-answer" AND p.post_author='.$user_id.' and p1.meta_value not in ( Select DISTINCT ID from '.$wpdb->prefix.'posts where post_type = "dwqa-question" and post_status = "private" )';
        $result = $wpdb->get_results( $query );
        $answer_count = $result[0]->answer_count;
        return $answer_count;
    }
}

For your need to show on the buddypress profile, you can add it to the file bp-custom.php file and in the function : bp_core_new_nav_item, next to the name , you can add a span contain the count like this : 'name' => __(sprintf( 'Questions<span class="count">%s</span>', $question_count), 'social-learner'),
I’ll help you implement it then .

nobita
replied 4 years ago

Hi, you can check your site again, and i made the change in the bp-custom.php file

rd08
replied 4 years ago

Hi, i checked. It is showing total count of all questions asked by all users on each user’s profile.
For example, visit http://www.civilcoursify.com/members-2/sneha/questions/
This user has asked 3 questions till now but the count shows 8 questions and 7 answers.
Also, the text in answers tab says that "has not asked any question". I would like it to be "has not answered any question".

nobita
replied 4 years ago

@Rohit D : fixed then, you can check all of it again.

rd08
replied 4 years ago

It is producing a bug. When a logged out user views another user’s profile the count is blank.
For example, visit: http://www.civilcoursify.com/members-2/sneha/

nobita
replied 4 years ago

Hi, fixed it then, just remove the criteria is_user_login()

theyuv
replied 3 years ago

Hello, I tried using count_user_posts() but it didn't work. Specifically, I think it fails because get_post_type_object() fails for 'dwqa-question' and 'dwqa-answer'. Is there a fix for this? Should I register these post types?
Thanks.

Allen
replied 3 years ago

Hi, so sorry for my late reply to you, well you could try my function there.
Also check this part of the function you use :
https://codex.wordpress.org/Function_Reference/count_user_posts

also can use this function

function count_user_posts_by_type( $userid, $post_type = ‘post’ ) {
global $wpdb;

$where = get_posts_by_author_sql( $post_type, true, $userid );

$count = $wpdb->get_var( “SELECT COUNT(*) FROM $wpdb->posts $where” );

return apply_filters( ‘get_usernumposts’, $count, $userid );
}

Allen
answered 3 years ago

Hi, so sorry for my late reply to you, well you could try my function there.
Also check this part of the function you use :
https://codex.wordpress.org/Function_Reference/count_user_posts
also can use this function
function count_user_posts_by_type( $userid, $post_type = ‘post’ ) {
global $wpdb;
$where = get_posts_by_author_sql( $post_type, true, $userid );
$count = $wpdb->get_var( “SELECT COUNT(*) FROM $wpdb->posts $where” );
return apply_filters( ‘get_usernumposts’, $count, $userid );

Powered by DW Question & Answer Pro