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

Display popular posts by views in WordPress without a plugin. In this tutorial, you will learn how to display popular posts by views in PHP WordPress without a plugin.

Sometimes you may want to show a popular post widget on your PHP WordPress site. This is a great way to get your readers familiar with the most popular content from this article or post. Content you want to show a post or page. So, this tutorial will show you how you can keep and display the most popular posts in WordPress without a plugin.

Note that there are many plugins available for PHP WordPress CMS (Content Management System). But it will have unnecessary functions and this can slow down the loading of your WordPress site. If you want more control, you want to keep the speed of the end site fast. So you will write your code to create such a plugin. And this will not affect the speed of your site.

Prerequisites

PHP 7.3.5 – 7.4.3, WordPress 5.5 – 5.7, MySQL 8.0.17 – 8.0.22

Keep Post Views Count

As a first step you need to store view counts for each post in a custom field. The following function will do the thing you need. Either you can create in the theme’s or child theme’s functions.php file or in the plugin file.

function tutsmake_set_post_views($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);
    }
}

In the above function I have specified a meta key (post_views_count) that is used to keep track of the view counts for each post. Next I have retrieved the count for the current post which is being viewed by the user. If count is not found then I am first deleting the meta key if exists and adding the meta key with count 0.

Now the above function can be called inside a single post loop:

tutsmake_set_post_views(get_the_ID());

Or you can make the things easy and put the below code snippets in the theme’s or child theme’s function.php file:

function tutsmake_track_post_views ($post_id) {
    if ( !is_single() ) return;
	
    if ( empty ( $post_id) ) {
        global $post;
        $post_id = $post->ID;    
    }
	
    tutsmake_set_post_views($post_id);
}

add_action( 'wp_head', 'tutsmake_track_post_views');

You need to get rid of pre-fetching and you can do so by the following line of code otherwise you will not see the correct count for post views:

remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

Finally, Display your popular posts on a WordPress site. So, you can use the following code snippets. You ideally may want to add the below code into your plugin to show it on the widget area.

//Set the parameters / arguments for the query
$popular_post_args = array(
	'meta_key'  => 'post_views_count', //meta key currently set
	'orderby'    => 'meta_value_num', //orderby currently set
	'order'      => 'DESC', //order currently set
	'posts_per_page' => 5 // show no. of posts
);

//Initialise the Query and add the arguments
$popular_posts = new WP_Query( $popular_post_args );

if ( $popular_posts->have_posts() ) :
?>
	
    have_posts() ) : $popular_posts->the_post(); ?>
  • " title="">

The post with the highest view counts will be on the top and rest are in order.

Recommended PHP Tutorials

Images mentioned above related to WordPress are either copyright property of respective image owners.

Rabins Sharma Lamichhane

Rabins Sharma Lamichhane is senior ICT professional who talks about #it, #cloud, #servers, #software, and #innovation. Rabins is also the first initiator of Digital Nepal. Facebook: rabinsxp Instagram: rabinsxp

Leave a Reply

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