Here’s a start (Function found here);
function 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 View";
}
return $count.' Views';
}
function 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);
}
}
// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
Now add this to your single.php file to track views;
<?php setPostViews(get_the_ID()); ?>
Now you can use the meta field post_views_count
to create a simple WP_Query loop that shows the 10 posts with the highest number of views. Eg;
$args = array(
'posts_per_page' => 10,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
$top_posts = new WP_Query($args);
while ($top_posts->have_posts()) : $top_posts->the_post();
// Your post loop content
endwhile;
wp_reset_postdata();