WordPress Display Popular Posts by Views Without Plugin

WordPress Display Popular Posts by Views Without Plugin

In this tutorial, you will learn method to display popular posts by views in PHP WordPress without plugins. By implementing this technique, you can create a popular post widget on your PHP WordPress site, providing your readers with easy access to the most popular content from your articles or posts. This approach ensures better control over your site’s speed and eliminates the unnecessary functions that can slow down your WordPress site.

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.

WordPress Display Popular Posts by Views Without Plugin

Follow the step-by-step instructions below to seamlessly integrate this feature into your WordPress site.

  • Step 1: Storing View Counts in a Custom Field
  • Step 2: Updating the Functions.php File
  • Step 3: Displaying Popular Posts
  • Step 4: Styling and Customization

Step 1: Storing View Counts in a Custom Field

Open your theme’s functions.php file or create a child theme if necessary. And Add the provided code snippet (shown below) to the functions.php file:

function track_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);
    }
}
function get_popular_posts($posts_num) {
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => $posts_num,
        'meta_key' => 'post_views_count',
        'orderby' => 'meta_value_num',
        'order' => 'DESC'
    );
    $popular_posts = new WP_Query($args);
    return $popular_posts;
}

Step 2: Updating the Functions.php File

Locate the loop in your theme’s index.php file (or any file where you want to display popular posts). And add the following code inside the loop to track post views:

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

Step 3: Displaying Popular Posts

In the desired location (e.g., sidebar, footer, or a specific template file), insert the following code to display popular posts:

<?php
$popular_posts = get_popular_posts(5); // Change '5' to the number of popular posts to display
if ($popular_posts->have_posts()) :
    while ($popular_posts->have_posts()) : $popular_posts->the_post();
        // Customize the display of each popular post
        ?>
        <div class="popular-post">
            <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
            <p><?php the_excerpt(); ?></p>
        </div>
        <?php
    endwhile;
endif;
wp_reset_postdata();
?>

Step 4: Styling and Customization

Adjust the HTML and CSS within the code snippet provided in Step 3 to match your site’s design and layout. And customize the display of each popular post as per your preference by modifying the HTML structure and adding relevant CSS styles.

Recommended PHP Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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