Add 'count' to questions widget

Any ideas on how I can add a 'count' feature to the questions widget? For example, if I'm showing popular questions I'd like to show the number of answers (like the 'mini-count' on the questions list) there are after the question title.

I'm trying to use 'add_filter' and then 'str_replace', but I don't know what to hook 'add_filter' on and I'm not sure what function outputs the answer count (is it 'the_question_status').


  • mattbailey

    Well I've done a bit more experimenting and I've managed to do it myself. I'm not sure if this is the best way, but it works. Everything takes place in my child theme 'functions.php' file. Here's a summary of what I did...

    First of all I copied the function 'the_question_status()' into 'functions.php', changed its name to 'the_question_count()' and edited the output so it just shows a span with the number of answers.

    Secondly, I copied the class 'QA_Widget_Questions' into 'functions.php' and renamed it to extend the original class. I then found the bit of code that outputs the widget html and changed this, adding in my new 'the_question_count()' function where required.

    I then copied the 'qa_widgets_init()' over and changed its name and edited it to use my new edited class.

    Finally I removed the original widget call from its phase and activated my new call.

    I'm not sure if that makes sense and I'm sure it could all be edited to be much shorter code (I suspect I don't need to copy the whole lot of the 'QA_Widget_Questions' class over for example), but it serves my purpose for now.

    Here's my new code:

    // Get the question count
    function the_question_count( $question_id = 0 ) {
    	if ( !$question_id )
    		$question_id = get_the_ID();
    	$count = get_answer_count( $question_id );
    	if ( get_post_meta( $question_id, '_accepted_answer', true ) )
    		$status = 'answered-accepted';
    	elseif ( $count > 0 )
    		$status = 'answered';
    		$status = 'unanswered';
    	echo	"<span class='question-count $status'>" . number_format_i18n( $count ) . "</span>";
    // New questions widget
    class QA_Widget_Questions_new extends QA_Widget_Questions {
    	var $default_instance = array(
    		'title' => '',
    		'which' => 'recent',
    		'number' => 5
    	function QA_Widget_Questions() {
    		$widget_ops = array( 'description' => __( 'List of questions', QA_TEXTDOMAIN ) );
    		$this->WP_Widget( 'questions', __( 'Questions', QA_TEXTDOMAIN ), $widget_ops );
    	function content( $instance ) {
    		global $post;
    		extract( $instance );
    		switch ( $which ) {
    			case 'recent':
    				$args = array();
    			case 'popular':
    				$args = array( 'meta_key' => '_answer_count', 'orderby' => 'meta_value_num' );
    			case 'unanswered':
    				$args = array( 'qa_unanswered' => true );
    		$args = array_merge( $args, array(
    			'post_type' => 'question',
    			'posts_per_page' => 5,
    			'suppress_filters' => false
    		) );
    		echo '<ul>';
    		foreach ( get_posts( $args ) as $post ) {
    			setup_postdata( $post );
    			//echo _qa_html( 'li', _qa_html( 'a', array( 'href' => get_permalink() ), get_the_title() ) );
    			echo '<li>';
    			echo _qa_html( 'a', array( 'href' => get_permalink() ), get_the_title() );
    			echo ''.the_question_count().'</li>';
    		echo '</ul>';
    	function form( $instance ) {
    		$instance = $this->parse_instance( $instance );
    		$this->title_field( $instance['title'] );
    			<label for="<?php echo $this->get_field_id('which'); ?>"><?php _e( 'Which:', QA_TEXTDOMAIN ); ?></label>
    			<select id="<?php echo $this->get_field_id('which'); ?>" name="<?php echo $this->get_field_name('which'); ?>">
    				$options = array(
    					'recent' => __( 'Recent', QA_TEXTDOMAIN ),
    					'popular' => __( 'Popular', QA_TEXTDOMAIN ),
    					'unanswered' => __( 'Unanswered', QA_TEXTDOMAIN ),
    				foreach ( $options as $value => $title ) {
    					$attr = compact( 'value' );
    					if ( $instance['which'] == $value )
    						$attr['selected'] = 'selected';
    					echo _qa_html( 'option', $attr, $title );
    			<label for="<?php echo $this->get_field_id('number'); ?>"><?php _e( 'Number of questions to show:', QA_TEXTDOMAIN ); ?></label>
    			echo _qa_html( 'input', array(
    				'type' => 'text',
    				'size' => 2,
    				'id' => $this->get_field_id('number'),
    				'name' => $this->get_field_name('number'),
    				'value' => $instance['number']
    			) );
    	function update( $new_instance, $old_instance ) {
    		$instance = $old_instance;
    		$new_instance = $this->parse_instance( $new_instance );
    		$instance['title'] = strip_tags( $new_instance['title'] );
    		$instance['which'] = $new_instance['which'];
    		$instance['number'] = (int) $new_instance['number'];
    		return $instance;
    function qa_widgets_init_new() {
    	if ( !is_blog_installed() )
    	register_widget( 'QA_Widget_Questions_new' );
    	register_widget( 'QA_Widget_Tags' );
    	register_widget( 'QA_Widget_Categories' );
    // Removes 'qa_widgets_init' from the 'widgets_init' phase
    function remove_qa_widgets_init() {
    // Call 'remove_qa_widgets_init' during WP initialization
    // Add our custom function to the 'thematic_header' phase
  • mattbailey

    I can in fact confirm that you can indeed remove most of the 'QA_Widget_Questions' class code when you copy it over - it seems you only need to edit the bit you require to be changed (in my case just one of the functions in the class).

    You learn something new everyday (I'm new to classes) :slight_smile:

    Hope this helps anybody else looking to get stuck in with some child-theming.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.