Home / Admin / Post Views Count | Post Views Tracking with Shortcode and Admin Column
Duplicate Snippet

Embed Snippet on Your Site

Post Views Count | Post Views Tracking with Shortcode and Admin Column

This WordPress code provides a comprehensive solution for tracking post views. It includes: cookie-based view counting to prevent duplicates, a [post_views] shortcode for front-end display, and a "Views" column in the admin area for easy overview.

Want to show trending posts ticker?
You can get the snippet "WordPress Trending Posts | Popular Posts Ticker Shortcode" from Snippets Library.

Code Preview
php
<?php
/*
Description: 
Enhance your WordPress site with a post view tracking system. This snippet uses a one-hour cookie to avoid inflating counts, offers a get_post_views() function for developers, a [post_views] shortcode for users, and adds a sortable "Views" column to your post list in the admin.
Key elements covered in these descriptions:
Core Functionality: Tracking post views. metakey "post_views_counting"
Prevention of Duplicate Counts: Creates a cookie for 1 hour to avoid multiple counting from current user.
Display Options: Shortcode [post_views] for front-end, column in admin.
Ease of Use: Simple implementation.
Benefits: Understanding content popularity, tracking engagement.
Want to show trending posts ticker? 
You can get the snippet "WordPress Trending Posts | Popular Posts Ticker Shortcode" from Snippets Library.
*/
// Function to track post views
function track_post_views($post_id) {
    if (!is_single() || (is_user_logged_in() && current_user_can('manage_options'))) { //bypass admin view count
    return;
    }
    
    $key = 'post_views_counting';
    $current_views = get_post_meta($post_id, $key, true);
    $current_views = $current_views ? $current_views : 0;
    
    // Prevent multiple views in a short period
    $cookie_name = 'post_viewed_' . $post_id;
    if (!isset($_COOKIE[$cookie_name])) {
        setcookie($cookie_name, 'true', time() + 3600, '/'); // Expires in 1 hour
        update_post_meta($post_id, $key, $current_views + 1);
    }
}
// Hook into WordPress template
function track_views_on_single_post() {
    if (is_single()) {
        global $post;
        track_post_views($post->ID);
    }
}
add_action('wp', 'track_views_on_single_post');
// Function to display view count
function get_post_views($post_id) {
    $key = 'post_views_counting';
    $views = get_post_meta($post_id, $key, true);
    return $views ? $views : 0;
}
// Shortcode to display post views
function post_views_shortcode($atts) {
    global $post;
    return get_post_views($post->ID);
}
add_shortcode('post_views', 'post_views_shortcode');
// Add post views column to admin
function add_views_column($columns) {
    $columns['post_views'] = 'Views';
    return $columns;
}
add_filter('manage_posts_columns', 'add_views_column');
function display_views_column($column_name, $post_id) {
    if ($column_name === 'post_views') {
        echo get_post_views($post_id);
    }
}
add_action('manage_posts_custom_column', 'display_views_column', 10, 2);

Comments

Add a Comment