How to Create a Unique Pageviews Counter in WordPress

How to Create a Unique Pageviews Counter in WordPress featured image

Reading Time: 3 min, 31 sec

Tracking pageviews is a great way to understand which posts or pages are performing well on your WordPress site. Most people use Google Analytics to track traffic and pageviews. But having a built-in solution in WordPress help you better understand how your articles are performing. While there are many plugins available, you can create a custom, lightweight, and accurate pageviews counter without relying on third-party tools. In this guide, I will show you how to do it step by step.

Before we starting making a custom solution, we need to understand a few things. A simple pageview counter that increments on every page load is easy to implement, but it inflates numbers because each reload counts as a new view. To get more accurate data, we need to track unique visitors. This can be done using cookies and by excluding admins or editors from being counted.

Best WordPress Hosting

Bluehost is one of the best and secure WordPress hosting provider

Step 1: Create a Function to Count Unique Views

Add the following code using a safe method, such as a Code Snippets plugin or a custom plugin, instead of editing functions.php directly:

function my_increment_unique_post_views($post_id) {
    $count_key = 'my_post_views';
    $cookie_name = 'my_post_view_' . $post_id;

    if(!isset($_COOKIE[$cookie_name])) {
        $count = get_post_meta($post_id, $count_key, true);
        if($count == '') {
            $count = 0;
            add_post_meta($post_id, $count_key, 1);
        } else {
            $count++;
            update_post_meta($post_id, $count_key, $count);
        }
        setcookie($cookie_name, '1', time() + 86400, COOKIEPATH, COOKIE_DOMAIN);
    }
}

This function checks if the visitor has already viewed the post in the last 24 hours. If not, it increments the count and sets a cookie.

Step 2: Trigger the Function on Single Posts

We want to count views only for single posts and exclude admin users:

function my_track_unique_post_views() {
    if(is_single() && !current_user_can('manage_options')) {
        global $post;
        if($post) {
            my_increment_unique_post_views($post->ID);
        }
    }
}
add_action('wp_head', 'my_track_unique_post_views');

Step 3: Display Pageviews on Your Site

To show the pageviews count anywhere on your theme:

$count = get_post_meta(get_the_ID(), 'my_post_views', true);
echo $count ? $count . ' Views' : '0 Views';

You can place this above or below the post title, inside single.php, or even in a shortcode.

Pros and Cons of This Method

This method is lightweight and fast because it does not rely on heavy plugins. It counts unique views accurately using cookies and gives you full control over how the counter works. Since the code is independent of your theme, it persists even when switching or updating themes, making it reliable for long-term use.

But there are also a few cons. Because it uses cookies, visitors who clear cookies or access your site from multiple devices may be counted multiple times. For very high traffic websites, this method may need optimization to avoid database load. Also, it does not track detailed analytics like referrers, geolocation, or time-based patterns without additional coding.

Tips for High Traffic Sites

For sites with heavy traffic, updating pageviews on every page load can slow down your database. One solution is to use WordPress transients, which store temporary counts in the cache and update the database periodically. This reduces the number of database writes while keeping the counter mostly accurate.

If your site receives very high traffic, consider using a custom database table instead of post meta. This keeps pageview data separate, avoids bloating wp_postmeta, and allows faster queries. You can also add extra columns for daily or monthly counts if needed.

Additionally, make sure to exclude bots from being counted and adjust cookie duration to control how often the same visitor is counted. These small improvements help ensure your pageviews counter remains accurate and performs well under heavy load.

Conclusion

Creating a custom pageviews counter in WordPress is simple, lightweight, and effective. By tracking unique visitors, you avoid inflated numbers and gain a more realistic view of your content’s performance. This method gives you full control and allows future enhancements like popular post lists or analytics dashboards.

If you want an even more effective and optimized pageview counter, or a custom-built solution tailored for your website, feel free to contact me. Also, let me know in the comments if you want me to share a ready-to-use advanced version with caching and performance improvements for high traffic sites.

Deepanker profile image

Written by Deepanker

Deepanker Verma is an experienced WordPress developer who has been working on WordPress for more than 12 years. On TheWPGuides, he writes about WordPress, WordPress development, and WordPress plugins.

Leave a Reply

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

Need a Hand with Your WordPress Site?

Don't let technical issues slow you down. Our professional WordPress maintenance service handles updates, security, and performance optimization so you can focus on what you do best: creating content.

Learn More About Our Services