WordPress Get and Set Page Visit Count

Tracking Post Views in WordPress (Without Plugins)

WordPress is one of the most widely used CMS platforms in PHP-based web development. It powers everything from simple blogs to complex websites and even full eCommerce platforms.

While WordPress offers a lot out of the box, many features can be extended or customized through functions.php.

A common requirement is tracking post or page views to identify popular content. In this guide, I’ll show a simple way to implement a post view counter without relying on external plugins.

How It Works

The approach uses WordPress post meta to:

  • Store view counts per post
  • Increment the count on each visit
  • Retrieve and display the count
  • Query posts based on popularity

Step 1: Set Post View Count

This function increments the view count every time a post is visited:

function wpb_set_post_views($postID) {
    $count_key = 'wpb_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);
    }
}

Step 2: Track Views Automatically

Hook into WordPress to track views on single post pages:

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');

Optional cleanup to remove unnecessary adjacent post links:

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

Step 3: Get Post View Count

To display the view count in your templates:

function wpb_get_post_views($postID) {
    $count_key = 'wpb_post_views_count';
    $count = get_post_meta($postID, $count_key, true);

    if ($count == '') {
        return "0 Views";
    }

    return $count . " Views";
}

Usage:

echo wpb_get_post_views(get_the_ID());

Step 4: Query Most Popular Posts

You can use the stored meta value to fetch popular posts:

$popularpost = new WP_Query(array(
    'posts_per_page' => 4,
    'meta_key' => 'wpb_post_views_count',
    'orderby' => 'meta_value_num',
    'order' => 'DESC'
));

while ($popularpost->have_posts()) : $popularpost->the_post();
    the_title();
endwhile;

wp_reset_postdata();

Note: If you are using a caching plugin, this technique will not work by default. If you are using W3 Total Cache, you can usea feature called Fragmented Caching to make this work just fine. For that just set view count using the following

< !-- mfunc wpb_set_post_views($post_id); -->< !-- /mfunc -->
#Credit: wpbeginner.com

Final Thoughts

This lightweight approach gives you full control over tracking post popularity without relying on third-party plugins.

However, for high-traffic sites, consider more scalable solutions such as:

  • Storing counts in a custom table
  • Using AJAX-based tracking
  • Integrating analytics tools

For most small to medium projects, this method is simple, effective, and easy to maintain.