Jack Lee
asked 10 years ago

/**
*
* Get user's best answers amount.
* @see dwqa_get_the_best_answer() in the file dw-question-answer\inc\actions.php.
* @package Customer's project.
* @since v1.0
* @author suifengtec
* @license GPL V3
* @param int:optional
* @return int|false
*/
function dwqa_count_best_answers_for_user($user_id=false){

$user_id=(int)$user_id;
if(!$user_id){

$user_id=(int)get_current_user_id();
}

if(!$user_id) return false;

global $wpdb;
$post_db=$wpdb->prefix.'posts';
$answer_ids=$wpdb->get_results("SELECT `ID`
FROM `{$post_db}`
WHERE `post_type`='dwqa-answer'
AND `post_author`={$user_id}
");

if( !$answer_ids ) return false;

$best_answer_amount=0;

foreach($answer_ids as $answer_id){

$question_id = get_post_meta( $answer_id->ID, '_question', true );
$best_answer_id = dwqa_get_the_best_answer( $question_id );

if ( $best_answer_id && $best_answer_id == $answer_id->ID ){

$best_answer_amount++;

}

}

return $best_answer_amount;

}
1 Answers
DominicStaff
answered 10 years ago

Awesome ! 😀 I appreciate for your help. 

Powered by DW Question & Answer Pro