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

At the blog spot site of mine it has been very easier to show popular posts y simply modifying the text. I WordPress the process is complicated as most of these views are to be transferred from the database to wordpress ever and for this majority of system administrator had been using different plugins to show popular posts in views. It has been easier to show popular posts by comments but that should not be the sole criteria to find popular posts. Most of these popular posts ratings should have been in the form of post views and in this article we should learn about it, how to do it without needing anything, sort of plugins.

A simple two ways coding is needed to display it. I installed the appearance code on sidebar dot php and this popular posts are viewed in the side bar and if you want to use it just after the end of the article then you could install that too. Popular posts are the signs of providing information to users about which article are most popular and in this way. It provides additional information for users to stay on the site for some more time.

Most of these plugins if we use for determining popular posts seem to us so much of difficult algorithms so that it ultimately makes the performance of the site slower and many a times understanding the way these plugins work seems nothing short of the most puzzled configurations. Most of these configurations and the provided informations seems to be not necessary and for this it is important to use such codes and learn the mechanisms of these so that ultimately everything should control the users.

Some sites have comments disabled and plenty of plugins are based upon the number of comments and this is not so good ways to find out information about this. If some sites do get lesser number of comments than most times the popular posts column remains as it is. The known and the common way to see to it that it should be dynamic. Not all users provide comments but there are some massive popular views on some sites and for this it is important to use this article while making these configurations. This article should show you the most basic ways to find out the number of views of posts and then while using this the popularity of posts are counted. This process is to be done with two way methods. The first step is to count the number of views and then the second step is to show which are popular posts.

Web administrator should edit codes from hosting panel so that if at all something wrong happens then it can be immediately modified without any delay. That also makes your site to remain online always. One could easily edit and add code of these files through WordPress administration but still it is better to do with from the server side.
After logging into a server, move to cPanel hosting and then browse fie manager from files sub segment.

Then right click on file manager and open in new tab and it would open, file manager and then go to wp-content and double click on it to open it. Then go to themes and double click on it to open it and then double click on the currently active theme from the list of theme present in the wordpress. Double click on the currently activated theme to open different theme files. Then open functions dot php file. Remember to keep the back up of these files on to your computer and then open it.

In order to open it, just select the file and from the top menu of file manager select code editor, and it will show you a pop up box and then click on edit and it would show functions dot php in the form of file editing. While doing this through the server you have to connect to internet. This is an application to self-hosted wordpress sites which have a domain and host attached with it.


// Popular Posts @ https://mohanmekap.com/2017/05/how-to-display-popular-posts-by-views-in-wordpress-without-a-plugin/

function mohanmekap_popular_posts($post_id) {
 $count_key = 'popular_posts';
 $count = get_post_meta($post_id, $count_key, true);
 if ($count == '') {
 $count = 0;
 delete_post_meta($post_id, $count_key);
 add_post_meta($post_id, $count_key, '0');
 } else {
 $count++;
 update_post_meta($post_id, $count_key, $count);
 }
}
function mohanmekap_track_posts($post_id) {
 if (!is_single()) return;
 if (empty($post_id)) {
 global $post;
 $post_id = $post->ID;
 }
 mohanmekap_popular_posts($post_id);
}
add_action('wp_head', 'mohanmekap_track_posts');

Here is the code and paste it at the end of functions dot php file and saves it. Now, do not open your website as it might not show as the work is incomplete and half done. Now we should move towards the second step of showing popular posts on website. First I would tell you about how to show it at the side bar. The display of it should be in accordance with the cascading style sheets and other formats which you have been using with website. It might change from site to site. For reference you could see how this code look like and display of informations regarding popular posts.

The above code has two functions working in tandem. The first function checks the number of post views of each and every post. The second function, checks and calls the first person whenever the single post is loaded and this means when a post is loaded the second function begins working and inform the first function and the first function count its post views. This code is simple yet works nicely. It works on the basis of normal function where we could find the presence of loading of individuals pages and then counting on these pages.

Display Popular Posts:

 

Popular Posts of mohanmekap.com

It is simple and with the following code pasted any where such as in sidebar dot php for showing it on side bar, in home dot php if you want to show only in home page and not in single pages of article and in single dot php to show at the end of the article. First we would learn to show in the side bar and for reference you could watch this side and could see the side bar to know more about it. The code is as follows,

 

<h3>Popular Posts</h3>
<ul>
 <?php $popular = new WP_Query(array('posts_per_page'=>7, 'meta_key'=>'popular_posts', 'orderby'=>'meta_value_num', 'order'=>'DESC'));
 while ($popular->have_posts()) : $popular->the_post(); ?>
 <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
 <?php endwhile; wp_reset_postdata(); ?>
</ul>

 

From theme files open sidebar dot php. I am using grandstand theme. It is important to back up php file before use.

Then, find the code

<div class="sidebar">

And then find the code,

dynamic_sidebar('blog');?>

or something similar to these codes and then put the above code in between these two and then save it and refresh your site and if everything goes in the right manner then am sure it should begin to show you at the sidebar. In the first, it should show one or two posts in popular post category but slowly it should begin to show you more posts as its function keep on counting the number of posts for the time being.

By using the second form of the code you could easily display popular posts in various regions. It is another form of letting users know about it that there are certain links to your site which are quite popular and users should have a look at it and this could increase visitor’ s returning to your site for far more time.

*A few days ago, I read a post on digwp which discussed the idea of inserting ads after a few paragraph. I’d never heard of this before, but I was intrigued, so I read on.*

This entry was posted in Wordpress Tutorial on by .

About Mohan Manohar Mekap

Mohan Manohar is a blogger from India who founded Ittech back in 2007. He is passionate about all things tech and knows the Internet and computers like the back of his hand.

One thought on “How to Display Popular Posts by Views in WordPress without a Plugin

Leave a Reply to Gabi Cancel reply

Your email address will not be published. Required fields are marked *