Well, depending on what you mean by top 5 posts. You can get top most read, top commented, most liked etc. For comments you can just make your query with argument
orderby=comment_count
If you want the most viewed, first you need to add the views counter like this:
/********* Post Views counter ***********/
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
if(!function_exists('mytheme_setPostViews')){
function mytheme_setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
}
if (!function_exists('mytheme_getPostViews')) {
function mytheme_getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0";
}
return $count;
}
}
And in your single.php
you’ll add after the begging of the loop
<?php if (have_posts()) : while (have_posts()) : the_post();
mytheme_setPostViews(get_the_ID());
To make the counter count.
Then to pull out the most viewed posts make a shortcode like
<?php
// Usage: [most_read_articles title="" post_no="" class=""]
function mytheme_most_read_posts( $atts, $content ){
extract(shortcode_atts(array(
'title' => '',
'post_no' => '',
'class' => '',
), $atts));
$pop_arg = array(
'posts_per_page' => $post_no,
'post_type' => 'post',
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
)
?>
<div class="most_read <?php echo $class; ?>">
<div class="most_read_title"><?php echo $title; ?></div>
<?php
$popularpost = new WP_Query( $pop_arg );
$i = 1;
if( $popularpost->have_posts() ) :
while ( $popularpost->have_posts() ) :
$popularpost->the_post();
?>
<div class="most_read_post">
<div class="most_read_post_info">
<span class="view_rank"><?php echo $i.'. '; ?></span><a href="https://wordpress.stackexchange.com/questions/218275/<?php the_permalink(get_the_ID()); ?>" class="most_read_post_title"><?php echo get_the_title(get_the_ID()); ?></a><span class="post_views">(<?php echo mytheme_getPostViews(get_the_ID()); ?>)</span>
</div>
</div>
<?php $i++;
endwhile;
endif;
wp_reset_postdata(); ?>
</div>
<?php
}
add_shortcode( 'most_read_articles', 'mytheme_most_read_posts');
You could modify this how you like ofc. But the methodology is the same.