• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
logo

All Blogging Tips

Everything About Blogging

  • Home
  • Start a Blog
  • Blogging Resources
  • About
  • Contact
  • Work With Me

Add Post Views Counter In WordPress Posts Without Plugin

By Ammar Ali 24 Comments

Add Post Views Counter In WordPress Posts Without PluginDid you ever want to know how many times a particular post has been viewed? I will show you How To Add Post Views Counter In WordPress Posts Without Plugin. This is very useful to track post views this will help you in future to write more interesting, quality content articles which can attract more visitors. Just add the following snippets to your template files and you are ready to go for Post Views Counter In WordPress!

I am not responsible if you done any mistake. So Check this video to Adding Code Though File Manager

 

Step 1: Adding Post Views Counter Code in Functions.PHP
  • Go to Appearance > EditorHow to Edit Functions.PHP
  • Under Editor section Select Functions.php in the list of php files on the right of your screen (or just see image below)
  • Paste Below code in functions.php
// Display or Count how many times a post has been viewed. // id = the post id and action = display or count function arixWp_PostViews( $id, $action ) { $axCountMeta = 'ax_post_views'; // Your Custom field that stores the views $axCount = get_post_meta($id, $axCountMeta, true); if ( $axCount == '' ) { if ( $action == 'count' ) { $axCount = 0; } delete_post_meta( $id, $axCountMeta ); add_post_meta( $id, $axCountMeta, 0 ); if ( $action == 'display' ) { echo "0 Views"; } } else { if ( $action == 'count' ) { $axCount++; update_post_meta( $id, $axCountMeta, $axCount ); } else { echo $axCount . ' Views'; } } }
  • After adding above code Click Update File
Step 2: Showing Posts Views Counter in WordPress Posts
  •  Select Single.PHP from right of your screen (See image below)

Single.PHP Screenshot

  • Now Paste below code just after High Lighted Section in above image.
 
    <?php echo arixWp_PostViews( get_the_ID(), 'count' ); ?>

To display the count outside the loop use this:

  <?php echo arixWp_PostViews( get_the_ID(), 'display' ); ?>

If you want them to show in the loop use this instead.

 <?php echo arixWp_PostViews( $post->ID, 'display' ); ?>
  • Finally Click on Update File and You had done.

Check your blog post to check whether counter is working or not!. Feel Free to Ask any question about this Post Views Counter In WordPress

<credit>

Filed Under: How To, WordPress Tagged With: count, Wordpress

About Ammar Ali

A blogger, web designer, front-end developer and WordPress specialist since 2011. I started this blog during high school. Here I share what I've learned so far and what I continue to learn through blogging so that I can be of assistance in some way to improve your blog. Read more here.

Reader Interactions

Comments

  1. Allii Allee says

    August 2, 2013 at 9:28 am

    No, But thanks bro i have installed WP-PostViews plugin what you recommend to Bilal Akbar

    Reply
  2. Allii Allee says

    August 1, 2013 at 3:20 pm

    i added the code in Functions.php but post views are not showing is it my theme problem or something else ?

    Reply
    • Ammar Ali says

      August 2, 2013 at 12:30 am

      Had you done adding the code in single.php too?

      Reply
  3. James says

    June 27, 2013 at 1:05 am

    I have added the code I used to a pastebin in case anyone needs it, I did change a few things. . . . but all in all its about the same code.

    pastebin.com/r8XSYLwB

    Thanks for the great work!!

    Reply
  4. paco says

    March 13, 2013 at 10:45 am

    The first time I view a single post, the counter displays 0 Views. I’m viewing it! I am the first viewer, I want to be counted right now!
    maybe changing:
    if ( $action == ‘display’ ) { echo “0 Views”;
    for
    if ( $action == ‘display’ ) { echo “1 View (YOU!)”;

    Reply
  5. Ankit says

    February 21, 2013 at 12:13 am

    Thanks a lot dear for this hack. Can you please tell me how can I add post views counter in blogger blog for individual posts? Please reply!

    Reply
    • Ammar Ali says

      February 21, 2013 at 3:47 pm

      Hi Ankit, There is no article available on my blog about this. However, you can read this here in details

      Reply
  6. Zach says

    October 5, 2012 at 12:30 pm

    does this count for views from the index page as well? or just views on each individual post

    Reply
    • Ammar Ali says

      October 5, 2012 at 7:18 pm

      It counts just views on each individual post! :)

      Reply
  7. Bilal Akbar says

    September 19, 2012 at 10:33 pm

    Please tell after which line or where we have to insert the code in functions.php

    Reply
    • Ammar Ali says

      September 20, 2012 at 1:40 pm

      Please check image I have added above!

      IF you still can’t understand. I recommend you to install this plugin -> http://wordpress.org/extend/plugins/wp-postviews/screenshots/ :)

      Reply
  8. e-rik says

    July 20, 2012 at 3:06 pm

    Hi Anmar Ali,

    Thanks for this useful piece of code. I put the code in a custom plugin and in content-single.php (twenty eleven). Works perfect. I put the code in content-single.php in a div so styling is easy.
    I had to alter the function code a little as after copy/paste some line breaks were missing and it appeared all as a comment. After adding line breaks after
    // Display or Count how many times a post has been viewed.
    // id = the post id and action = display or count
    and
    // Your Custom field that stores the views
    all was fine.

    Reply
  9. DG says

    February 21, 2012 at 11:00 pm

    Hello there, thanks for the snippet and it works perfectly! :)

    but I have a one question, whether this Post views can still work well when combined with total w3 cache plugin?

    thanks in advance

    Reply
    • Ammar says

      February 22, 2012 at 1:43 pm

      It will be work with W3 cache. Because its a simple code not plugin!

      Reply
  10. DG says

    February 21, 2012 at 5:14 pm

    Thanks for useful tips, I’ll try it on my website… :mrgreen:

    Cheers!

    Reply
  11. Ne The says

    December 28, 2011 at 12:22 am

    Thanks a lot!
    So how can add most viewed posts without plugin?

    Reply
    • Ammar Ali says

      December 28, 2011 at 8:28 am

      I will write post on it. But you need to wait a little bit :)

      Reply
  12. mayank says

    December 21, 2011 at 7:51 pm

    where to add css and html codes of ]]b:skin in wordpress.

    Reply
    • Ammar Ali says

      December 22, 2011 at 2:45 pm

      You are using WordPress not Blogger and you there is not code ]]b:skin in WordPress

      Reply
  13. mayank says

    December 21, 2011 at 7:50 pm

    where to add css and html codes of ]]> in wordpress.

    Reply
    • Ammar Ali says

      December 22, 2011 at 2:46 pm

      You are using WordPress not Blogger and you there is no code ]]b:skin in WordPress

      Reply
  14. mayank says

    December 21, 2011 at 7:25 pm

    how to add css in wordpress

    Reply
    • Ammar Ali says

      December 22, 2011 at 2:47 pm

      Goto Appearance > Editor and select Style.PHP to add desired CSS code!

      Reply
      • irfanali says

        February 7, 2013 at 10:37 am

        Its Style.CSS not Style.PHP.I don’t see you tracking number of page views for this website.

        In between thanks a lot.Cheers

        Reply

Leave a Reply Cancel reply

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

Primary Sidebar

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Popular Posts

  • Is Your Blog Properly Optimized? 5 SEO Must-Haves to Check Off Your List
  • 10 Powerful Ways to Become a Famous Blogger in No Time!
  • 10 Proven Ways To Make Money Without Google AdSense
  • Google AdSense Vs Affiliate Marketing: Which One Should You Choose?
  • 5 Tactics To Boost Your Content Reach & Engagement
  • 12 Things to Do Before Applying for Google AdSense
  • Are Backlinks Becoming Less Valuable?
  • Step by Step Guide to Creating a Travel Blog
  • How to Start as a Freelance Writer & Is It Worth?
  • Add Smooth Multi Level Drop-Down Menu In Blogger

Let Me Help You!

Let me take care of your website and make it run like a champ. I can help you with blog setup, migration, quick fixes or maintenance services.

Work With Me

Copyright © 2026 · Genesis Sample on Genesis Framework · WordPress · Log in