How to Track Post Views in WordPress without a Plugin Using Post Meta

Home » Snippets » How to Track Post Views in WordPress without a Plugin Using Post Meta
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

WordPress, while a powerful platform, can sometimes be bogged down by too many plugins. So what do you do when you want to track post views, but don’t want to install another plugin? Enter post meta data – an easy, efficient way to keep track of post views directly in WordPress without bogging down your site’s speed. In this article, we will guide you through the process of setting up and using post meta to track post views without needing an additional plugin.

					function wpturbo_track_post_views($post_id) {
    if (!is_single()) return;
    if (empty ($post_id)) {
        global $post;
        $post_id = $post->ID;    
    }
    $current_views = get_post_meta($post_id, "wpturbo_view_count", true);
    if(!isset($current_views) OR empty($current_views) OR !is_numeric($current_views) ) {
        update_post_meta($post_id, "wpturbo_view_count", 1);
    } else {
        update_post_meta($post_id, "wpturbo_view_count", ++$current_views);
    }
}
add_action( 'wp_head', 'wpturbo_track_post_views');
				

The provided code snippet establishes a way to track post views in WordPress without a plugin by using post metadata.

In the first step, we define a function called wpturbo_track_post_views($post_id). The function employs a post ID as an argument. Identifying individual posts with their unique post ID enables us to track the views for each post separately.

In the section within the function:

if (!is_single()) return;
if (empty ($post_id)) {
    global $post;
    $post_id = $post->ID;    
}

These couple of conditions work jointly to ensure that the view count decrement function is only put into operation when a single post is displayed. The first line, if (!is_single()) return;, checks if a single post is being displayed. If not, the function returns nothing and stops executing any further.

The second condition, if (empty ($post_id)), ensures that there is an actual post ID to work with. If a post ID is not specified, the function will get the ID of the global $post object that WordPress sets up whenever it queries the database for posts.

Next, we have:

$current_views = get_post_meta($post_id, "wpturbo_view_count", true);
if(!isset($current_views) OR empty($current_views) OR !is_numeric($current_views) ) {
    update_post_meta($post_id, "wpturbo_view_count", 1);
} 

These lines perform the task of retrieving and verifying the current post view count. The get_post_meta() function retrieves the current view count for the post from the meta field named wpturbo_view_count. This meta field is specific to the actual post with the ID specified.

The if condition checks if the variable $current_views is set, not empty, and contains a numeric value. If any of these checks fail, the update_post_meta() function sets the view count to 1.

In the final part of the function:

else {
    update_post_meta($post_id, "wpturbo_view_count", ++$current_views);
}

This block of code is processed if the if check passes, meaninhg $current_views was correctly set and contained a numeric value. The view count is then incremented by one using the ++$current_views expression, and the update_post_meta() function updates the view count in the database.

Finally, we hook our function into the wp_head action:

add_action( 'wp_head', 'wpturbo_track_post_views');

This action hook ensures the wpturbo_track_post_views() function is called for every single post, and occurs in the head section of the WordPress website when a single post is being displayed. This is all done without the need of a plugin, solely using the power of WordPress Post Meta Data.

Register an account to save your snippets or go Pro to get more features.