Wordpress

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

In the past we have shown you how to create a popular post tabber in WordPress using a plugin. That plugin works great out the box for tabbers. However, we wanted more customization in our layout, so we decided to do it without a plugin. In this article, we will show you how to track and display popular posts by views in WordPress without using any plugins.

An example of our custom popular post display is shown in the screenshot below:

Popular Posts Example

First thing we need to do is create a function that will detect post views count and store it as a custom field for each post. To do this, paste the following codes in your theme’s functions.php file or better in a site-specific plugin:

01 function wpb_set_post_views($postID) {
02     $count_key = 'wpb_post_views_count';
03     $count = get_post_meta($postID, $count_key, true);
04     if($count==''){
05         $count = 0;
06         delete_post_meta($postID, $count_key);
07         add_post_meta($postID, $count_key, '0');
08     }else{
09         $count++;
10         update_post_meta($postID, $count_key, $count);
11     }
12 }
13 //To keep the count accurate, lets get rid of prefetching
14 remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

Now that you have this function in place, we need to call this function on the single post pages. This way the function knows exactly which post gets the credit for the views. To do this, you would need to paste the following code inside your single post loop:

1 wpb_set_post_views(get_the_ID());

If you are using a child theme or you just want to make things easy for yourself, then you should simply add the tracker in your header by using wp_head hook. So paste the following code in your theme’s functions.php file or the site-specific plugin:

1 function wpb_track_post_views ($post_id) {
2     if ( !is_single() ) return;
3     if ( empty ( $post_id) ) {
4         global $post;
5         $post_id = $post->ID;   
6     }
7     wpb_set_post_views($post_id);
8 }
9 add_action( 'wp_head', 'wpb_track_post_views');

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

Note: If you are using a caching plugin, this technique will NOT work by default. We are using W3 Total Cache, and it has the feature called Fragmented Caching. You can use that to make this work just fine. Here is what needs to be changed:

1 <!-- mfunc wpb_set_post_views($post_id); --><!-- /mfunc -->

Now, you can do all sort of cool stuff such as display post view count, or sort posts by view count. Lets take a look at how to do some of these cool things.

If you want to display the post view count on your single post pages (often next to the comment count or something). Then the first thing you need to do is add the following in your theme’s functions.php file or the site-specific plugin.

01 function wpb_get_post_views($postID){
02     $count_key = 'wpb_post_views_count';
03     $count = get_post_meta($postID, $count_key, true);
04     if($count==''){
05         delete_post_meta($postID, $count_key);
06         add_post_meta($postID, $count_key, '0');
07         return "0 View";
08     }
09     return $count.' Views';
10 }

Then inside your post loop add the following code:

1 wpb_get_post_views(get_the_ID());

If you want to sort the posts by view count, then you can do so easily by using the the wp_query post_meta parameter. The most basic example loop query would look like this:

1 <?php
2 $popularpost = new WP_Query( array( 'posts_per_page' => 4, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'wpb_post_views_count meta_value_num', 'order' => 'DESC'  ) );
3 while ( $popularpost->have_posts() ) : $popularpost->the_post();
4
5 the_title();
6
7 endwhile;
8 ?>

To add other WP_Query parameters such as time range, refer to the WP_Query page on Codex.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s