Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I created a plugin in wordpress to show popular posts, but this is not returning the post and number of comments properly.

Even I have comments in my post(approved as well, so this is not the case) its displaying -

"No commented post this time!!";

Although If i put echo at this line around Line 78 - $popular .= '</li>'; it shows popular post, although I think there is something wrong still(either with logic or in code).

Debug option is set true define('WP_DEBUG', true);

My code -->

<?php
/*
Plugin Name: Sw Popular Post Widget
Plugin URI: http://demo.test.com
Author: Swapnesh Kumar Sinha
Description: Go your Widget Section to check the widget. It is a general purpose plugin that creates a widget to limit the popular post display from 1 to 10 at front end of your site. Please leave your honest comments,suggestions, bugs etc regarding the plugin at my email id - "swapnesh20032003@gmail.com". The plugin is in working condition, tested at - WordPress 3.5.
########
INSTALL it via plugin section in your Wordpress Admin panel and then find it under APPEARANCE -> WIDGETS section.
######## 
Version: 1.0
Author URI: http://swapneshsinha.wordpress.com
*/

class SwPopularPostWidget extends WP_Widget {

public function __construct() {
    //Widget actual processes
    $widget_options = array( 'Description' => 'Show Popular Posts' );
    parent::__construct( 'SwPopularPostWidget', 'Sw Popular Post Widget', $widget_options); 
}

public function form( $instance ) {
    //outputs the options form on admin
$instance = wp_parse_args( (array) $instance, array( 'popularpostcount' => '','popularpostheading' => '' ) );
$popularpostcount = $instance['popularpostcount'];
$popularpostheading = $instance['popularpostheading'];
echo "Heading Title";   
echo "<input type='text' name='".$this->get_field_name('popularpostheading')."' id='".$this->get_field_name('popularpostheading')."' value='".$popularpostheading."' />";
echo "Number of post to show "; 
//echo "<select name='popularpostcount' id='popularpostcount'>";
echo "<select name='".$this->get_field_name('popularpostcount')."' id='".$this->get_field_name('popularpostcount')."'>";
    for( $i =1; $i<=10; $i++ )
    {
        if ( $i == $popularpostcount )
        echo "<option value='".$i."' selected>".$i."</option>";
        else
        echo "<option value='".$i."'>".$i."</option>";
    } 
echo "</select>";
}

public function update( $new_instance, $old_instance ) {
    //processes widget options to be saved
    $instance = $old_instance;
    $instance['popularpostcount'] = $new_instance['popularpostcount'];
    $instance['popularpostheading'] = $new_instance['popularpostheading'];
    return $instance;
}

public function widget( $args ,  $instance ) {
    //outputs the content of widget
    extract($args);
    $popularpostcount = apply_filters('widget_title', $instance['popularpostcount']);
    $popularpostheading = apply_filters('widget_title', $instance['popularpostheading']);
    echo '<aside class="widget widget_meta" id="popular-2">';
    echo  '<h3 class="widget-title">'.$popularpostheading.'</h3>';
    echo  "<ul>";
    echo $this->popularPosts($popularpostcount);
    echo  "</ul>";
}


public function popularPosts($num)
{
     global $wpdb;

    $posts = $wpdb->get_results("SELECT * FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , $num");
    //echo "<pre>";print_r($posts);die;
    foreach ($posts as $post) {
        setup_postdata($post);
        $id = $post->ID;
        $title = $post->post_title;
        $count = $post->comment_count;

        if ($count != 0) {
            $popular  = '<li>';
            $popular .= '<a href="' . get_permalink($id) . '" title="">' . $title.'</a> '." - ".$count." Comments";
            $popular .= '</li>';
        }
        else
        $popular = "No commented post this time!!";
    }
    return $popular;
}


}

add_action( 'widgets_init' , create_function('', 'register_widget( "swpopularpostwidget" );' ) );
share|improve this question
add comment

1 Answer 1

one possibility is:

public function popularPosts($num)
{
    global $post;
    $args = array( 'orderby' => 'comment_count', 'order' => 'DESC' );
    $posts = get_posts( $args );
    $count = 0;
    foreach ($posts as $post):
        if($post->comment_count != 0 ){
            $count++;
        }   
    endforeach;
    if ( $count > 0 ):
        foreach( $posts as $post ) : setup_postdata($post);
            if($post->comment_count != 0 ){ 
                ?>
                <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> - <?php echo $post->comment_count; ?> Comments</li>
                <?php
            }
        endforeach;
    else :
        echo '<li>No commented post this time!!</li>';
    endif;
}


}
function swpopularpostwidget_init() {
    if ( !is_blog_installed() )
        return;

    register_widget('swpopularpostwidget');
    do_action('widgets_init');
}
add_action( 'init' , 'swpopularpostwidget_init');
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.