How to display the most viewed posts on page

Viewed 118

I want to display the most viewed posts using dooplay theme.Except header and footer no posts appear only white blank screen appears. I am not sure either my query is not working or my functions which I added in functions.php are not working.I created metadata for my post type in functions.php and in second code of functions.php I add a tracker. Here is the code which I add in functions.php

function set_views($post_ID) {
    $key = 'views';
    $count = get_post_meta($post_ID, $key, true); //retrieves the count

    if($count == ''){ //check if the post has ever been seen

        //set count to 0
        $count = 0;

        //just in case
        delete_post_meta($post_ID, $key);

        //set number of views to zero
        add_post_meta($post_ID, $key, '0');

    } else{ //increment number of views
        $count++;
        update_post_meta($post_ID, $key, $count);
    }
  }

  //keeps the count accurate by removing prefetching
  remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

  Here is the code to track the function:

 function track_custom_post_watch ($post_ID) {
    //you can use is_single here, to track all your posts. Here, we're traking custom post 
 'watch'
    if ( !is_singular( 'watch') ) return; 

    if ( empty ( $post_ID) ) {

        //gets the global post
        global $post; 

        //extracts the ID
        $post_ID = $post->ID;    
    }

    //calls our previously defined methos
    set_views($post_ID);
  }
  //adds the tracker to wp_head.
  add_action( 'wp_head', 'track_custom_post_watch');

Here is the code of my template:


<?php
/* 
Template Name: populardrama
*/
get_header();

// Modules
$default = array(
'slider'        => false,
'featured-post' => false,
'movies'        => false,
'ads'           => false,
'tvshows'       => false,
'seasons'       => false,
'episodes'      => false,
'top-imdb'      => false,
'blog'          => false
);
// Options
$fullwid = dooplay_get_option('homefullwidth');
$modules = dooplay_get_option('homepage');
$sidebar = dooplay_get_option('sidebar_position_home','right');
$maxwidth = dooplay_get_option('max_width','1200');
$maxwidth = ($maxwidth >= 1400) ? 'full' : 'normal';

$hoclass = ($fullwid == true) ? ' full_width_layout' : ' '.$sidebar;
?>

<h3>Popular Posts</h3>

<?php
$query = new WP_Query( array(
        'post_type'     => 'movies', //your post type
        'posts_per_page' => 10, 
        'meta_key'      => 'views', //the metakey previously defined
        'orderby'       => 'meta_value_num',
        'order'         => 'DESC'
    )
);
$popularloop = new WP_Query( $movies );

while ($query->have_posts()) {
    $query->the_post();
    while ( $popularloop->have_posts() ) : $popularloop->the_post();
    ?>
    <a href="<?php the_permalink() ?>">
    <img src="<?php echo dbmovies_get_poster($post->ID); ?>" alt="<?php the_title(); ?>">
    </a>
    <a href="<?php the_permalink() ?>">
    <h3 class="rating"><?php the_title(); ?></h3>
    </a>
    <?php
    endwhile;
    
    wp_reset_postdata();
    
  }
?>



<?php
get_footer();
?>
1 Answers

Add on your function.php

<?php
   function count_post_visits() {
      if( is_single() ) {
         global $post;
         $views = get_post_meta( $post->ID, 'my_post_viewed', true );
         if( $views == '' ) {
            update_post_meta( $post->ID, 'my_post_viewed', '1' ); 
         } else {
            $views_no = intval( $views );
            update_post_meta( $post->ID, 'my_post_viewed', ++$views_no );
         }
      }
   }
   add_action( 'wp_head', 'count_post_visits' );
?>

Add this code on your template file where you want to display

<?php
   $popular_posts_args = array(
      'posts_per_page' => 3,
      'meta_key' => 'my_post_viewed',
      'orderby' => 'meta_value_num',
      'order'=> 'DESC'
   );
   $popular_posts_loop = new WP_Query( $popular_posts_args );
   while( $popular_posts_loop->have_posts() ):
      $popular_posts_loop->the_post();
   endwhile;
   wp_reset_query();
?>
Related