web developement

Tracking Post Views Without a Plugin in WordPress Using PHP

Monitoring how many times your posts are viewed can give you valuable insights into what content your visitors engage with the most. While there are many plugins available for this purpose, you can easily implement post view tracking in WordPress using just a few lines of PHP. In this article, you’ll learn how to track and display post views without relying on any external plugin.

Why Track Post Views?

Tracking post views can help you:

  • Identify your most popular content.
  • Optimize your editorial strategy.
  • Enhance SEO by promoting high-performing pages.
  • Provide social proof to visitors by showing view counts.

Let’s dive into the code.

Step 1: Create Functions to Set and Get Post Views

We’ll use WordPress post meta to store the view count. Add the following functions to your theme’s functions.php file or in a custom plugin:

// Set post views
function wpb_set_post_views($postID) {
    $count_key = 'wpb_post_views';
    $count = get_post_meta($postID, $count_key, true);
    if ($count === '') {
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, 1);
    } else {
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

// Get post views
function wpb_get_post_views($postID) {
    $count_key = 'wpb_post_views';
    $count = get_post_meta($postID, $count_key, true);
    return ($count === '') ? '0 Views' : $count . ' Views';
}

These two functions handle storing and retrieving the number of views for each post using a custom meta key called wpb_post_views.


Step 2: Increment Views When a Post is Viewed

Now, let’s hook into WordPress to update the view count when someone visits a single post. Add this to functions.php:

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

This function checks if the current page is a single post and then calls the view increment function.

Note: The function runs on every page load for single posts, so it may slightly increase database writes if your traffic is high.


Step 3: Display the View Count on Your Site

You can display the post view count anywhere in your theme, such as within single.php, content.php, or directly in a template file:

echo wpb_get_post_views(get_the_ID());

If you’re using the block editor and want to display views within a block theme, you can create a shortcode:

function wpb_post_views_shortcode() {
    return wpb_get_post_views(get_the_ID());
}
add_shortcode('post_views', 'wpb_post_views_shortcode');

Now you can use [post_views] in your content to show view counts.


Optional: Hide the Meta Key from Custom Fields UI

To prevent the custom meta key from showing in the “Custom Fields” meta box (if you’re using classic editor), you can prefix it with an underscore:

$count_key = '_wpb_post_views';

Just make sure you update all references to the key in your functions.


Optional: Prevent Count from Increasing for Bots and Admins

To reduce false view counts from bots or logged-in users, you can enhance the tracking logic:

function wpb_track_post_views($post_id) {
    if (!is_single() || is_admin() || current_user_can('edit_posts')) return;
    if (empty($post_id)) {
        global $post;
        $post_id = $post->ID;
    }
    wpb_set_post_views($post_id);
}

Conclusion

With just a few PHP functions, you can implement a lightweight and efficient post view tracking system directly in your WordPress theme or custom plugin. This method avoids the overhead of plugins and gives you full control over how and where view counts are displayed.

Whether you use it for reporting, boosting popular content, or enhancing user engagement, tracking post views is a valuable feature you can easily build on your own.

Convenient hosting for your WordPress sites

Looking for good hosting for your WordPress sites? Pay attention to Host4Biz. It is a reliable hosting with modern servers in Europe and a Ukrainian team.

And with the promo code MYHOST10 you will get a 10% discount on your first payment. To do this, register here and enter the code before paying.

Another great option for WordPress hosting is Hostinger. Register your account via the link https://hostinger.com.ua?REFERRALCODE=1VOLODYMYR55 and follow updates in my blog

Note: There are affiliate links in the link given above and if you buy something, I’ll get a commission at no extra cost to you.

Leave a Reply

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