Skip to content
Be-smart

Be-smart

  • Home
  • Blog
    • Most Visited Posts
    • Stories
    • Unisex
      • Kids
      • Man
      • Woman
    • ADS
    • Social Media
  • About
    • Contact
  • Login

Track and display post views, using a custom function php

Photo of author
Flora
07/01/2025
Views: 24

Estimated Reading Time: 2 min

To track and display post views in WordPress, you can use a custom function. Below is the code for implementing this functionality:


Step 1: Add Code to Functions.php

  1. Open your WordPress dashboard.
  2. Go to Appearance → Theme File Editor.
  3. Select the functions.php file of your theme (or child theme).
  4. Add the following code:


// Function to set post views
function set_post_views($postID) {
    $count_key = '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);
    }
}

// Function to get post views
function get_post_views($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if ($count == '') {
        return "0 Views";
    }
    return $count . ' Views';
}

// Hook to increment views when a single post is viewed
function track_post_views($post_id) {
    if (!is_single()) return;
    if (empty($post_id)) {
        global $post;
        $post_id = $post->ID;
    }
    set_post_views($post_id);
}
add_action('wp_head', 'track_post_views');



  

Step 2: Display Post Views Function

To display the post views on your site, add the following code where you want the views to appear, such as in the single.php template file:

<?php echo get_post_views(get_the_ID()); ?>

Step 3: Avoid Counting Admin Views (Optional)

To prevent admin views from being counted, update the track_post_views function like this:



function track_post_views($post_id) {
    if (!is_single() || current_user_can('manage_options')) return;
    if (empty($post_id)) {
        global $post;
        $post_id = $post->ID;
    }
    set_post_views($post_id);
}
add_action('wp_head', 'track_post_views');



  

Step 4: Styling and Placement

You can place the display code in templates like:

  • single.php to show views on individual post pages.
  • content.php or index.php to show views in the blog feed.

Use CSS to style the views display if needed.


Would you like help integrating this into your theme or adding additional features like caching?

Categories BSB, Html, WordPress
Skip on not skip WordPress 404 Handling For Static Files
I am block on TikTok at 200-300 views
Share on Facebook Share on WhatsApp Share on X (Twitter) Share via Email

Related Posts

  • WordPress Function php ad include php
  • From the podium of the miss finalists, puree in a blender
  • How to clear quick access any recent file and frequently folders in Windows
  • The most known and respected people post mortem in Lithuania
Photo of author

Flora

Track and display post views, using a custom function php

Published 07/01/2025

Contact

  • X
  • TikTok
  • Facebook
  • WhatsApp
I am Flora, the publisher and founder of *Be-Smart*, a platform dedicated to sharing insights and inspiration for living a fulfilling life. With a strong background in the web, my goal is to empower people to genuinely recognize and celebrate admirable actions big or small in themselves and others.

Bitcoin Price (BTC)

$104,929.68

Download BTC Ticker

Trending Posts

  • Here are ten of the most iconic and celebrated BMW models of all time
  • BMW M4 Competition (F82): Redefining Performance and Style
  • How to Clear Cache BlueHost with Cron Job Command
  • Legendary ‘Killing Me Softly’ singer, dead at 88
  • Euro 2024 Qualification Groups: All You Need to Know

Recent Posts

  • Fresh and Healthy Heart of Palm Salad Recipe
  • Ultimate WordPress Countdown Timer: PHP, CSS & JavaScript Guide
  • How to create a related posts section
  • How to uninstall Windows programs using a batch script
  • Here are 5 cool WhatsApp tricks you might not know about:

Archives

  • March 2025 (1)
  • February 2025 (65)
  • January 2025 (94)
  • December 2024 (42)
  • November 2024 (69)
  • October 2024 (92)
  • September 2024 (31)

Recent Comments

No comments to show.

Partners Group

Amazon AliExpress BlueHost
GeneratePress SnepBelgium Takeaway

BlogRoll

Mirror

The MixedZone

Finance Digest

  • Facebook
  • X
  • WhatsApp
  • RSS Feed
  • Facebook
  • X
  • WhatsApp
  • RSS Feed

BSB (228) Celebrity (24) Europe (37) Food (14) Html (41) Lifestyle (23) Love (18) Medical (15) Movies (18) My Health (28) PC World (36) PHP (22) Science (15) Social Media (13) Software (14) Stories (96) Weekly Horoscopes (13) Windows (29) Woman (20) WordPress (76)

  • Privacy Policy
  • Terms and Conditions
  • Disclaimer
  • Cookie Policy

© 2024 Be-smart

All Rights Reserved

Built with GeneratePress

This website uses cookies to ensure you get the best experience. By continuing to browse, you agree to our Privacy Policy.