Many WordPress plugins make your work easy but so many use of different types of plugins will slow down your website speed. Some little and important task done by writing little code and with small effort. Here we learn to get popular posts by views in WordPress without using any plugin. Just write some code and apply some steps according to our post and you will get popular posts by views. We will show you how to track and get popular posts by views wordpress without using any plugins.By using this code you don’t have no need to use any plugin to show popular posts by views in wordpress.After implementation this code your site automatically optimized and your loading speed will be fast.


You May Also Like:


Here we Know How To Get Popular Posts by Views in WordPress without a Plugin

First of all to create a function which is used to detect post views count and store it. Paste this code in your theme’s function.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);

Put this code in  your single.php inside the loop

 

wpb_set_post_views(get_the_ID());

If you are using a child theme so you should simply add a tracker in your header by using wp_head hook. This below lines of code paste in your themes function.php file.

 

function thr_track_post_views ($post_id) {
    if ( !is_single() ) return;
    if ( empty ( $post_id) ) {
        global $post;
        $post_id = $post->ID;    
    }
    thr_set_post_views($post_id);
}
add_action( 'wp_head', 'thr_track_post_views');

Once you have placed this, every time a user visits the post, the custom field will be updated.

 

<!-- mfunc thr_set_post_views($post_id); --><!-- /mfunc -->

If you want to display the post view count on your single post then paste this following code in your theme’s functions.php file

function thr_get_post_views($postID){
    $count_key = 'thr_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';
}

you can easily sort the posts by view count, then you can do so easily by using the the wp_query post_meta parameter.

<?php 
$popularpost = new WP_Query( array( 'posts_per_page' => 4, 'meta_key' => 'thr_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC'  ) );
while ( $popularpost->have_posts() ) : $popularpost->the_post();

the_title();

endwhile;
?>

About The Author

Related Posts

2 Responses

  1. joe

    Does this count pages too? I need pages and posts counted and displayed

    Reply

Leave a Reply

Your email address will not be published.