How to display most viewed related post

hi there i am looking for a plugin or a code to display most viewed related post.
in fact i want to display most viewed related post in my single.php.
i am using the following code to show the related post

    <?php
02  //for use in the loop, list 5 post titles related to first tag on current post
03  $tags = wp_get_post_tags($post->ID);
04  if ($tags) {
05  echo 'Related Posts';
06  $first_tag = $tags[0]->term_id;
07  $args=array(
08  'tag__in' => array($first_tag),
09  'post__not_in' => array($post->ID),
10  'posts_per_page'=>5,
11  'caller_get_posts'=>1
12  );
13  $my_query = new WP_Query($args);
14  if( $my_query->have_posts() ) {
15  while ($my_query->have_posts()) : $my_query->the_post(); ?>
16  <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
17   
18  <?php
19  endwhile;
20  }
21  wp_reset_query();
22  }
23  ?>

and for the most viewed post i’m using this method

<?php
function wpb_set_post_views($postID) {
        $count_key = 'wpb_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);
        }
    }
    //To keep the count accurate, lets get rid of prefetching
    remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
    function wpb_track_post_views ($post_id) {
        if ( !is_single() ) return;
        if ( empty ( $post_id) ) {
            global $post;
            $post_id = $post->ID;   
        }
        wpb_set_post_views($post_id);
    }
    add_action( 'wp_head', 'wpb_track_post_views');
    function wpb_get_post_views($postID){
        $count_key = 'wpb_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 View";
        }
        return $count.' Views';
    }

is there a way to mix this two function or is there a plugin to do this for me.

Read more here:: How to display most viewed related post

Leave a Reply

Your email address will not be published. Required fields are marked *