WordPress Display Popular Posts by Views Without Plugin

Last Updated on by in Web Development

WordPress 5.5 display popular posts by views tutorial; This quick guide we will explain how you can show or display popular posts or articles by views in WordPress sites without using a third-party plugin.

WordPress is the most popular content management system; creating a website in less time is only possible with WordPress. It offers tons of plugins, ready-made themes with top-notch tutorials and support.

However, custom plugins and themes come with excessive code, and it is considered good practice to create any specific feature without the plugin.

You will find out how to display popular posts by views in WordPress without an external plugin, and we will create this feature using few lines of code that consists of WordPress custom function and methods.

A site owner’s primary task is to enhance user engagement; many tips and tricks a site owner can apply to accomplish this task.

One way is to show the most popular post of your site to your site visitors, and you might put the most popular posts in a sidebar or just before the article is concluded.

But do you know how to show popular post of your site to site readers? Well, this is why we are here to help you with this.

We will share some of the custom functions through which you can create a popular post by views widget without a plugin in WordPress. After completing this quick guide, you will have a profound idea of displaying the most popular posts by views in WordPress without a plugin.

When it comes to showing popular posts by views in wordpress using a plugin, this feature can be injected into WordPress within minutes. You can download the plugin in your wordpress project. However, this process directly impacts your site speed because you don’t know how heavy the used plugin’s size is.

To get rid of the site speed issue, we create the simple feature to display posts by views in wordpress in the simple way.

Requirements

  • PHP 7.3.5 or above
  • WordPress 5.5 or greater
  • MySQL 8.0.17 or above

Store Post Views Count

In the initial stage we have to head over to functions.php file, In the main functions file we have to store the views count for each and every post or article within a custom field.

So, make sure to place following code at the bottom of the function file, you may place the suggested code in child theme or main themes functions.php configuration file.

<?php
/**
 * Positron functions and definitions
 *
 */

function positronx_set_post_views($post_id) {
    $count_key = 'wp_post_views_count';
    $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);
    }
}

Inside the positronx_set_post_views() custom function, we passed the $post_id variable which represents the current post id. The wp_post_views_count meta key is keeping an eye on views count for every post.

Further, we fetched the current post count for every view generated through a user. Nevertheless, we have written the functions if there is no count found then we are removing the meta key and setting the meta key count to zero.

Get Current Post View Count

Furthermore, you may use the following function and place within the single post loop query to get the current post views.

positronx_set_post_views(get_the_ID())

We can also place the following code inside the functions.php config file, the adjacent_posts_rel_link_wp_head is solving the pre-fetching issue. In simple terms it will display post views count properly and accurately.

function positronx_track_post_views ($post_id) {
    if ( !is_single() ) 
    return;
     
    if ( empty ( $post_id) ) {
        global $post;
        $post_id = $post->ID;    
    }
     
    positronx_set_post_views($post_id);
}
 
add_action( 'wp_head', 'positronx_track_post_views');


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

Show Popular Posts by View in WordPress

Theoretically, you have to place this code where you will show popular posts by views, it might be sidebar, or any widget.

<?php

$popularpostbyview = array(
    'meta_key'  => 'wp_post_views_count', // set custom meta key
    'orderby'    => 'meta_value_num',
    'order'      => 'DESC',
    'posts_per_page' => 4
);
 
// Invoke the query
$prime_posts = new WP_Query( $popularpostbyview );
 
if ( $prime_posts->have_posts() ) :?>
    <ul>
        <?php
            while ( $prime_posts->have_posts() ) : $prime_posts->the_post();
            ?>
                <li>
                    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                      <?php the_title(); ?>
                    </a>
                </li>
            <?php
            endwhile;
            wp_reset_postdata();
        ?>
    </ul>
<?php 
endif;

Ultimately, we have reached to the last segment of this example, let me walk through the below code one by one.

The $popularpostbyview variable holds the properties where we have added custom meta key, set the orderby prop to evoke the current post order, and posts per page which shows the total number of popular posts.