Display Popular Posts by Views Without a Plugin in WordPress

Displaying popular posts in WordPress was an amazing experience when I developed a code snippet for my client. Code snippet is related to display popular posts by views not by comments. There are many WordPress plugins for giving us such type of functionality. But, plugins can make our site heavier. This code snippet has the ability to create a custom field name post_views_count dynamically in your post and increment the custom field value when the post is clicked or viewed. So, now I’m telling you how to display popular posts by views without a plugin in WordPress.

 

1. Set Post Views

Open your functions.php and paste the following code.

[code type=”php”]

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);
}
}

[/code]

Explanation:

Line 1: Declare a function and pass the current the post id.

Line 2: Saving custom field name ‘post_views_count‘ in a variable. You can choose your own custom field name.

Line 3: Saving custom field value in a variable $count.

Line 4: Check if custom field value is empty.

Line 5: Set $count to zero.

Line 6: Delete post meta.

Line 7: And set custom field value to zero.

Line 8: Else

Line 9: Increase custom field value by 1.

Line 10: Update post meta value to the increased value.

Where to Put the Code:

In your single.php inside the loop, put the following code.

[code type=”php”]

observePostViews(get_the_ID());

[/code]

Now we are all set and saving the post clicked value in a custom field which is directly going in the database. But we are not done yet, now we have to fetch the values and query the loop accordingly.

2. Fetch Post Views

In your functions.php paste the following function beneath observePostViews function.

[code type=”php”]
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’;
}[/code]

Explanation:

Line 1: Declare a function like we did above.

Line 2: Saving custom field name in a variable.

Line 3: Saving custom field value in a variable.

Line 4: Check if custom field value is empty.

Line 5: Delete custom field.

Line 6: Set custom field value to 0.

Line 7: Return custom field value or we can say counted value.

Where to Put the Code:

If you want to show just views counter, paste the following code inside loop in your single.php.

[code type=”php”]<?php fetchPostViews(get_the_ID()); ?>[/code]

But this tutorial is regarding displaying popular posts without a plugin, so paste the following code where you want to show popular posts.

[code type=’php’]

<?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(); ?>

[/code]

I hope you will find this post useful. Thanks.

Duan Lingxin

Duan Lingxin

Content crafter and chief editor at Scratching Info. Also regular contributor on other major online tech platforms. Security Specialist by day and a writer by night, he does his best to instill his knowledge about tech while delivering inspiring and life changing resources through his writing,

More Posts - Website

9 thoughts on “Display Popular Posts by Views Without a Plugin in WordPress”

  1. Thank you very much! This is very useful!

    I do not know how to display 20 records in 24 hours … Maybe you tell me?

    Thank you again! 🙂

  2. Great tutorial! Is it also possible to make a plugin out of this? I want to create something similar to this and then put a shortcode to a page that activates this function. Could you please help me out or give me suggestions. Thanks!

  3. Hello and thank you for the fantastic tutorial! Worked great for me!
    I am experiencing a problem though. For some strange reason WP has stopped counting my views for the posts after I have implemented your method.

    Regards,
    Tommer.

  4. Hello. Great tutorial. For some reason WP stopped counting my posts..
    I omitted the

    because then I will get an error which will display a huge amount of posts of the same post. I do have my posts visible and ordered thought.

    • This method is still working for me. This method will create a custom field “post_views_count” for every post.

      I think a plugin is causing problem, and might be that plugin is also creating custom field “post_views_count”. So you’re getting errors.

Leave a Comment