HOMEBlogWordPressEasy Guide to Add a WordPress Post Views…

Easy Guide to Add a WordPress Post Views Counter Programmatically

If you want to track how many times your articles are being read, adding a WordPress Post Views Counter directly inside your theme is one of the cleanest and fastest approaches. Instead of relying on heavy third-party plugins, you can insert a lightweight custom function that counts real visits and (optionally) excludes crawler hits. In this guide, I’ll show you a simple, developer-friendly method to implement post view tracking programmatically.

Why Add a Post Views Counter Manually?

There are many plugins that can track views, but they often come with unnecessary scripts, database writes, or large settings panels. Writing your own view counter gives you:

  • Full control over how and when views are counted
  • Less load on your WordPress installation
  • Clean code that works with any theme
  • Flexibility to exclude bots and crawlers

Step 1: Add the View Counter Function in functions.php

Create two functions: one to increment the view count, and one to fetch it for display.

PHP
// Increase post views
function pn_set_post_views( $postID ) {
    $views = get_post_meta( $postID, 'pn_post_views', true );

    if ( empty( $views ) ) {
        $views = 0;
    }

    $views++;
    update_post_meta( $postID, 'pn_post_views', $views );
}

// Get post views
function pn_get_post_views( $postID ) {
    $views = get_post_meta( $postID, 'pn_post_views', true );
    return ! empty( $views ) ? $views : 0;
}

This will store your WordPress Post Views count inside a custom post meta field named pn_post_views.

Step 2: Trigger Post Views Counter on Single Post Loads

You should increase the view counter only when viewing a single post (not archives or pages).

Add this to functions.php:

PHP
function pn_track_post_views() {
    if ( is_single() ) {
        global $post;
        pn_set_post_views( $post->ID );
    }
}
add_action( 'wp_head', 'pn_track_post_views' );

This ensures the view increments every time a visitor actually opens the post.

Optional: Exclude Bot and Crawler Visits

You likely don’t want bots inflating your WordPress Post Views count. A simple way is to check the user agent before counting.

Add this helper:

PHP
function pn_is_crawler() {
    $crawler_list = array(
        'bot', 'crawl', 'spider', 'slurp', 'facebookexternalhit', 'mediapartners-google'
    );

    $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] ?? '' );

    foreach ( $crawler_list as $crawler ) {
        if ( strpos( $agent, $crawler ) !== false ) {
            return true;
        }
    }

    return false;
}

Then modify the tracking function:

PHP
function pn_track_post_views() {
    if ( is_single() && ! pn_is_crawler() ) {
        global $post;
        pn_set_post_views( $post->ID );
    }
}
add_action( 'wp_head', 'pn_track_post_views' );

Now only human visitors trigger a view count.

Step 3: Display the Post Views Counter in Your Theme

To show the view count inside your theme template (single.php, content-single.php, etc.):

PHP
<?php echo 'Views: ' . pn_get_post_views( get_the_ID() ); ?>

Final Thoughts

Adding a WordPress Post Views Counter programmatically is lightweight, fast, and gives you full control over how your data is stored and displayed. This approach avoids plugin bloat while still providing accurate real-user metrics.

Abhik

🚀 Full Stack WP Dev | ☕ Coffee Enthusiast | 🏍️ Biker | 📈 Trader
Hi, I’m Abhik. I’ve been coding since 2007, a journey that began when I outgrew Blogger and migrated to a robust self-hosted stack. That transition introduced me to WordPress, and I’ve been building professional solutions ever since.

Leave a comment