LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » WordPress / WordPress Tricks » How To Get Popular Post By Views For WordPress Without Plugin?

How To Get Popular Post By Views For WordPress Without Plugin?

How-To-Get-Popular-Post-By-Views-For-WordPress-Without-Plugin
WordPress comes with many features but its missing a valuable features that is a per post counter. This is needed by nearly everyone but still new updates of WordPress posts did not have this. This helps bloggers to show there most popular posts by page views to next visitor so he will also read that link other CMS have this option by default.

Presenting popular posts throughout WordPress was an incredible experience when My partner and i developed a program code snippet for my own client. Code snippet is related to display popular threads by views certainly not by comments. There are several WordPress plugins for giving us such style of functionality. But, plugins might make our site more substantial. This code snippet has the ability to create a customized field name post_views_count dynamically inside your post and increment the actual custom field value if your post is visited or viewed. So, now I’m letting you know how to show popular posts by views and not using a plugin in WordPress.

Table of Contents

How To Save Popular Post By Views For WordPress Without Plugin?

Step 1:) Copy the below code and Paste in your Theme Function(function.php) file.

/* ------------------------------------------------------------------------- *
* Get Popular Post By Views For WordPress (Set Page Views)
/* ------------------------------------------------------------------------- */
function observePostViews($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);
}
}
/* ------------------------------------------------------------------------- *
* Get Popular Post By Views For WordPress (Fetch Page Views)
/* ------------------------------------------------------------------------- */
function fetchPostViews($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';
}

Step 2:) Now Copy the below code and Paste in your Single Post(single.php) file inside the loop and this will save your post views in database.

<?php observePostViews(get_the_ID()); ?>

Step 3:) Its done.

How To Show Popular Post By Views For WordPress Without Plugin?

Step 1:) Copy the below code and Paste in your Single Post(single.php) file inside the loop and this will show your that post views from database to visitor.

<?php fetchPostViews(get_the_ID()); ?>

How To Get Popular Post By Views For WordPress Without Plugin?

Step 1:) Copy the below code and Paste in your any theme files like Sidebard,Single,Page etc file and this will only fetch the most views post list from database to visitor.

<?php
query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC');
?>
<?php while(have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_post_thumbnail(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>

Customization:

You do not have to change anything.

Recommended For You:
How Videos Can Help In Your Website SEO?

Theme Files Editing Warning:

Keep in mind that every code that you will add in your themes files will be removed when you will change the theme. So after changing your theme, you have to follow this article again and you will get everything back same as it is now.

Last Words:

This is what we have and shared in easy steps for newbies so that they can easily know how it works. Stay with us because we are going to share a whole guide step by step about WordPress and make it easy for you. If you liked it then share it and be with us to get next tutorial. If you have any problem then feel free to ask us. We will help you with what we can or have.

You Like It, Please Share This Recipe With Your Friends Using...

4 Responses to “How To Get Popular Post By Views For WordPress Without Plugin?”

  1. Syed says:

    Hi, Absolutely incredible article.I get more info after reading your post. Thanks for sharing !

  2. Jeff says:

    Hello,
    I want to display only 3 popular posts. How I do it, please ?
    Thank you

Leave a Reply

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