DIsplaying popular post for your visitor may increase some extra page view on your website, people are always showing interest for popular matters. this could help you to decrease bounce rate on your website analytics. low bounce rate is most key of SEO ranking on google search and other search engines. WordPress has many ready plugins for displaying popular post widget, here is the simple code snippet that’s could help you add popular post widget feature without any plugin.

How to display popular post widget?

<?php
add_action('widgets_init','register_dartthemes_popular_posts_widget');
function register_dartthemes_popular_posts_widget()
{
register_widget('dartthemes_popular_posts_widget');
}
class dartthemes_popular_posts_widget extends WP_Widget{
function dartthemes_popular_posts_widget()
{
parent::__construct( 'dartthemes_popular_posts_widget', __('Bug Blog - Recent Posts', 'bug-blog'), array('description' => __('Bug Blog recent posts widget to display recent posts', 'bug-blog')));
}
/*-------------------------------------------------------
*				Front-end display of widget
*-------------------------------------------------------*/
function widget($args, $instance)
{
extract($args);
$title 			= apply_filters('widget_title', $instance['title'] );
$count 			= $instance['count'];
$order_by 		= $instance['order_by'];
echo $before_widget;
$output = '';
if ( $title )
echo $before_title . $title . $after_title;
global $post;
if ( $order_by == 'popular' ) {
$args = array( 
'orderby' => 'meta_value_num',
'meta_key' => 'post_views_count',
'posts_per_page' => $count,
'order' => 'DESC'
);
} else {
$args = array( 
'orderby' => 'comment_count',
'order' => 'DESC',
'posts_per_page' => $count
);
}
$posts = get_posts( $args );
if(count($posts)>0){
$output .='<div class="latest-posts ' . $order_by . '">';
foreach ($posts as $post): setup_postdata($post);
$output .='<div class="media">';
if(has_post_thumbnail()):
$output .='<div class="pull-left">';
$output .='<a href="'.esc_url(get_permalink()).'">'.get_the_post_thumbnail( get_the_ID(), 'xs-thumb', array('class' => 'img-responsive')).'</a>';
$output .='</div>';
endif;
$output .='<div class="media-body">';
$output .= '<h3 class="entry-title"><a href="'.esc_url(get_permalink()).'">'. get_the_title() .'</a></h3>';
$output .= '<div class="entry-meta small">'.get_the_date('d M Y').' '.__('By:', 'bug-blog').' '. get_the_author() . ' </div>';
$output .='</div>';
$output .='</div>';
endforeach;
wp_reset_postdata();
$output .='</div>';
}
echo $output;
echo $after_widget;
}
function update( $new_instance, $old_instance )
{
$instance = $old_instance;
$instance['title'] 			= strip_tags( $new_instance['title'] );
$instance['order_by'] 		= strip_tags( $new_instance['order_by'] );
$instance['count'] 			= strip_tags( $new_instance['count'] );
return $instance;
}
function form($instance)
{
$defaults = array( 
'title' 	=> __('Latest Posts', 'bug-blog'),
'order_by' 	=> 'latest',
'count' 	=> 5
);
$instance = wp_parse_args( (array) $instance, $defaults );
?>
<p>
<label for="<?php echo esc_attr($this->get_field_id( 'title' )); ?>"><?php _e('Widget Title', 'bug-blog'); ?></label>
<input type="text" id="<?php echo esc_attr($this->get_field_id( 'title' )); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr($instance['title']); ?>" style="width:100%;" />
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id( 'order_by' )); ?>"><?php _e('Ordered By', 'bug-blog'); ?></label>
<?php 
$options = array(
'popular' 	=> __('Top view', 'bug-blog'),
'comments'	=> __('Most Commented', 'bug-blog')
);
if(isset($instance['order_by'])) $order_by = $instance['order_by'];
?>
<select class="widefat" id="<?php echo esc_attr($this->get_field_id( 'order_by' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'order_by' )); ?>">
<?php
$op = '<option value="%s"%s>%s</option>';
foreach ($options as $key=>$value ) {
if ($order_by === $key) {
printf($op, $key, ' selected="selected"', $value);
} else {
printf($op, $key, '', $value);
}
}
?>
</select>
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id( 'count' )); ?>"><?php _e('Count', 'bug-blog'); ?></label>
<input type="text" id="<?php echo esc_attr($this->get_field_id( 'count' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'count')); ?>" value="<?php echo absint($instance['count']); ?>" style="width:100%;" />
</p>
<?php
}
}

We have previously published how view count without any plugin may need, you need to change the meta key to compatible with post_views_count popular post widget.

How to add post view counter without plugin

Hope you enjoy the post. please post a comment if you have any trouble.