WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is the code to display popular post on front end, however the problem is the data is not saving from the back end. Like i want to show only 3 popular posts on front-end so i select the option 3 but its not saving. However when i pass the static value for $popularpostcount then this code works echo $this->popularPosts($popularpostcount); so i believe the problem is only saving the selected option.

I am new to plugin development so please let me know what mistake i am making while the development.

Plugin Name: Popular Post Widget
Plugin URI: http://demo.test.com
Author: Swapnesh Kumar Sinha
Description: This is a plugin to show popular posts in sidebar as a widget.
Version: 1.0
Author URI: http://swapneshsinha.wordpress.com

class PopularPostWidget extends WP_Widget {

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

public function form( $instance ) {
    //outputs the options form on admin
$instance = wp_parse_args( (array) $instance, array( 'popularpostcount' => '' ) );
$popularpostcount = $instance['popularpostcount'];  
echo $popularpostcount;
echo "Number of post to show "; 
echo "<select name='popularpostcount' id='popularpostcount'>";
    for( $i =1; $i<=5; $i++ )
        if ( $i == $instance['popularpostcount'] )
        echo "<option value='".$i."' selected>".$i."</option>";
        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'];
    return $instance;

public function widget( $args ,  $instance ) {
    //outputs the content of widget
    $popularpostcount = apply_filters('widget_title', $instance['popularpostcount']);
    echo  "<h2>Most Popular Posts</h2>";
    echo  "<ul>";
    echo $this->popularPosts($popularpostcount);
    echo  "</ul>";

public function popularPosts($num)
     global $wpdb;

    $posts = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , $num");

    foreach ($posts as $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;
            $popular .= '</li>';
    return $popular;


add_action( 'widgets_init' , create_function('', 'register_widget( "popularpostwidget" );' ) );
share|improve this question
up vote 3 down vote accepted

Replace this line in form function:

echo "<select name='popularpostcount' id='popularpostcount'>";


echo "<select name='".$this->get_field_name('popularpostcount')."' id='".$this->get_field_name('popularpostcount')."'>";
share|improve this answer

Note: in addition to Kilan's answer, I would also recommend using the WordPress selected() function:

echo "<select name='" . $this->get_field_name('popularpostcount') . "' id='popularpostcount'>";
    for( $i =1; $i<=5; $i++ )
        echo "<option " . selected( $i == $instance['popularpostcount'] ) . " value='" . $i . "'>".$i."</option>";
echo "</select>";
share|improve this answer

Your Answer


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.