display popular posts by views

How to Display Popular Posts by Views in WordPress Without a Plugin

You want to know How to display popular posts by views in WordPress without a plugin. There are too many plugins to display popular posts.  But we can do it with some PHP code. Lots of webmasters don’t want to use plugins for simple jobs. It’s the effect on website speed. WordPress Jetpack has there amazing popular post widgets.

In this article, We try to explore everything about popular posts. Firstly discover posts view. Because, If you want to know which post is popular? You must know how many views a post has. 

How To Get Posts View:

When visitors visit a post. We updated a meta key automatically. That’s the meta key count view. Create a function that does this job automatically.

Use this function on function.php

function wpk_set_post_views($postID) {
    $count_key = 'wpk_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);

We set a meta key name “wpk_post_views_count”. When posts are loaded the meta key value update. Now we track visitors for count views.

Add The Tracker for Posts View:

To track view, Put this function under the while loop in single.php 

wpk_set_post_views(get_the_ID());

But if you create a child theme. Or you don’t want to edit single.php. Add a tracker using wp_head. Use this function on function.php.

function wpk_track_post_views ($postID) {
    if ( !is_single() ) return;
    if ( empty ( $postID) ) {
        global $post;
        $postID = $post->ID;    
    }
    wpk_set_post_views($postID);

}
add_action( 'wp_head', 'wpk_track_post_views');

If you use the W3 Total Cache plugin for clearing cache. That time this technique was not working.  It has the feature called Fragmented Caching.   You can use like this-

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

Display Posts View:

If I want to display the Posts view in post meta or anywhere at single post.  We create a function for Viewing Post view. 

Use this function on function.php

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

Now just call wpk_get_post_views() on single posts while loop.  You will get a post view count.

Display Popular Post:

Now time displays popular posts. We have the meta key “wpk_post_views_count”. This meta key counts views.  Based on this count we will show a list of popular posts using WP_Query.

$args = array( 
	'posts_per_page' => 4, 
	'meta_key' => 'wpk_post_views_count', 
	'orderby' => 'meta_value_num', 
	'order' => 'DESC'  
);
$popularpost = new WP_Query($args);

if ($popularpost->have_posts()) {
	echo '<ul>';
	while ($popularpost->have_posts()) {
		$popularpost->the_post();
		echo '<li>';
		the_title();
		echo '</li>';
	}
	echo '</ul>';
};

You can use This algorithm for professional purposes but you need to know it well first. We hope you already know, How to Display Popular Posts by Views in WordPress Without a Plugin 

FAQs

What are popular posts?

Every blog site has the same posts that are most visited or visitors love it. The most visited posts are called popular posts. Popular posts in many ways, views and comments, and many more.  

Conclusion:

If you are learning theme development, This algorithm will help you to learn popular posts. If you have any problem with it, feel free to contact us or comment below.

See More:

How to Display Related Posts in WordPress Without Plugins

Leave a Reply

Your email address will not be published.