wordpress snippets last updated   : October 6, 2014
we now have 641 snippets

Track post views without a plugin using post meta

wordpress snippet

Use the following snippet to track post views on your wordpress blog. The first thing you want to do is add this snippet to the functions.php of your wordpress theme. Follow step 1. and step 2. to track and display the number of views for each post.

snippet :  PHP - functions.phpcopy
function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}
function setPostViews($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);
    }
}

// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); 

Step 1.

This part of the tracking views snippet will set the post views. Just place this snippet below within the single.php inside the wordpress loop.

snippet :  PHP - single.phpcopy
<?php
          setPostViews(get_the_ID());
?>

Step 2.

The snippet below is optional, so use this if you would like to display the number of views within your posts. Place this snippet within the loop.

snippet :  PHP - single.php / index.phpcopy
<?php 
          echo getPostViews(get_the_ID());
?>

Did you like this post? If so please share it with your friends. Thanks!

  • http://www.facebook.com/towfiqi Towfiq Ahmed

    Awesome share Chad. Thanks :D

  • http://www.facebook.com/towfiqi Towfiq Ahmed

    Do I have to set them manually?:/

    • http://wpsnipp.com Kevin Chard

      Yes you would need to add this code to your wordpress templates manually.

  • http://www.facebook.com/towfiqi Towfiq Ahmed

    Just found this post.
    http://www.moocr.com/blog/posts/2010/display-posts-views-count-without-plugin-in-wordpress.html

    Did what the guy said. Doesn’t seem to work. Does it work in localhost?

    • http://wpsnipp.com Kevin Chard

      It should work on local host fine, however you would need to reload the page to see any views. Don’t forget to place setPostViews within the loop within single.php. I tested it on wpsnipp.com to track views and it worked fine. What the snippet does is add a number to post meta and then increment that each time the single.php loads. What version of wordpress are you running?

    • http://www.facebook.com/towfiqi Towfiq Ahmed

      Thanks.Just found out that I wasn’t implementing it correctly.

    • http://wpsnipp.com Kevin Chard

      Cool glad you got everything working perfectly…

  • Bob

    Is it possible to make a list of mist viewed posts based on this code?

    • http://wpsnipp.com Kevin Chard

      I’m not sure if I understand, can you go into a little more detail.

    • Bob

      Example:
      Most viewed posts
      Post title3 100 views
      Post title1 50views
      Post title2 10 views

    • http://wpsnipp.com Kevin Chard

      Hi Bob,
      Yes I had thought about doing something like this, ill fool around with the idea for a bit and post a snippet.

  • Alec

    Is there a way to make an overview of the most viewed posts?
    Just like the populair in your sidebar, but with usage of the post view instead of the comments ?

    • http://wpsnipp.com Kevin Chard

      Hi Alec,
      I just finished a post for that yesterday and will be posting it in about an hour.

    • Alec

      First time i visit your site, and already my hero! +1

  • Pingback: 20 More WordPress Code Snippets and Hacks

  • http://www.facebook.com/people/Daniel-Tara/1041109969 Daniel Tara

    I would rename the post_views_count meta key to _post_views_count to make it hidden to avoid users editing the post views from custom fields.

    • http://wpsnipp.com Kevin Chard

      Ahh good call Daniel, you could do that.

  • http://pctip.info PCTip

    This technique won’t work with cache plugin. I would like using WP-PostViews plugin as it provides a better compatibility with cache plugin.

    • http://wpsnipp.com Kevin Chard

      This is true, it does not play nice with cache plugins. However as a quick snippet to track views it works quite well.

  • Pingback: Afficher le nombre de visite par article sans extension en utilisant la balise meta | Un Blog Presque Parfait

  • Pingback: Helpful Tips, Hacks and Tutorials about WordPress - Wordpress Arena

  • Wprebel

    I’m having an issue with this, if i view the second newest post the newest post get’s 1 view added, any ideas why?

    • http://wpsnipp.com Kevin Chard

      The only reason I can think of is that you don’t have the setPostViews(get_the_ID()); function within the single.php? can you confirm this is the case…

      • Wprebel

        Yeah it’s in the single.php in the loop

        • Nef426

          I am having the same problem after setting up a most popular posts list in the sidebar, using the “most Popular posts using views post meta” article. setPostViews(get_the_ID());  is in the single.php. setPostViews(get_the_ID());  is in the single.php. 

      • http://www.facebook.com/perfectweiss Samuel Francisco

        I’m also having an issue here. Mine is in single.php, I followed the instructions. However I notice that if I visit a post (with a post ID let’s say 20), another post (with post ID let’s say 22) also gets +1 on views count. The weird thing is, when I echo the post ID received by the function setPostViews, only the post ID 20 is displaying on my screen.

        • http://wpsnipp.com Kevin Chard

          Well it should work without any issues, however if you edit the posts you can see the custom field added with the current post views. I would reload the one page and see if the custom field updates on both posts. What version of wordpress are you currently running?

        • http://www.401creative.com Chris (4O1! Creative)

          I was having the same issue. It took me forever to figure it out. The problem is caused by WordPress loading relational links for the previous and next posts in the head. Firefox, by default, prefetches REL links with the value of Next so both posts are being “viewed”
          The solution I found was to remove the action that adds this functionality to the wp_head function: remove_action( ‘wp_head’, ‘adjacent_posts_rel_link_wp_head’, 10, 0); 

          • http://wpsnipp.com Kevin Chard

             Chris, thanks for the comment I updated the post to let others know about this fix for the issue. I also added a link to your site. Thanks again,

    • http://wpsnipp.com Kevin Chard

      Hi Wprebel, You may have a plugin or something else that caused a problem with the snippet. The snippet is fairly simple and just updates the post meta when a user loads the post, not much to go wrong. Without looking at your theme in detail I would just be guessing,

    • http://wpsnipp.com Kevin Chard

        @e4fcff6cc58dc5e1b2ab556d9cdb2cb2:disqus you need to use the getPostViews() function when you want to display post views and setPostViews() on the single.php.

  • http://www.psdbox.com Andrei Oprinca

    This is awesome, it works brilliantly. I will use it on a new wallpapers site I’m working on. Noted down your site on the functions for future reference.

    I wonder if something can be done to only record only 1 view per session for every user. That would be cool to prevent “fake” views when refreshing the page. If anybody can do that (if it’s even possible) that would be awesome.

    • http://wpsnipp.com Kevin Chard

      Thanks @6c9628c836d0d3ed67e8ef3b43a993dd:disqus Glad to hear you like the snippet! I’m sure something like that could be done, however I would use this as a method to track popularity of a post rather then for detailed stats.

  • Ahmedsheeraz

    hi nice information, is it possible to use echo getPostViews(get_the_ID()); in admin panel posts section if i want to use post views only for personal/admin purposes

  • Anonymous

    Needless to say, this is an awesome snippet. But if there is a solution to make it work with caching plugins, for instance with W3TC, it would be even better.

    Anyway, good work!

    • http://wpsnipp.com Kevin Chard

      I have not looked into this, ill take a closer look at some of the plugins and see what I can do. Glad you like the snippet though. 

      • Anonymous

        Hey Kevin, I’ve been browsing through forums to hit a solution. Apparently many people having problems with excluding functions from being cached. Unfortunately, there seems to be no cure any cure so far. I’m sure there is. Let’s hope one of us comes up with something soon :)

    • Nattila1

      hello, As i know W3TC has a “Ignored query stems” to filter out some functions from cache.

  • Andrew

    amazing, thank you!

    • http://wpsnipp.com Kevin Chard

      no problem @4f9649a2dc0c5d186623a62544564bfe:disqus glad you like the snippet!

  • http://tasarhane.com tasarhane designhouse

    hey, its cool..
    thanks a lot.

    • http://wpsnipp.com Kevin Chard

      Cool glad you like the snippet. 

  • Pingback: Site Review: WordPress Code Snippets | ButlerBlog

  • Sunnysuffy

    Its Just a fake nothing else, dude If you are checking in firefox it will show but if you checked your url in chrome and IE its not displaying there. When you check in firefox it will show only your views nothing else…and if you think its my problem, just tell me.

    • http://wpsnipp.com Kevin Chard

      Hi Sunnysuffy well the snippet is not fake, however if you have caching software running on your copy of wordpress it would have problems working. Supercache, etc.

  • Gabriel Merovingi

    Hey Kevin!

    Tried this code out on a fresh WP 3.2 install and it works nicely. I however noticed that all though it should only increate the count by 1 mine does it by 4. Any suggestions what could be wrong? 

    Got the functions set up in the right place and the only change I did to it was to exclude counts from logged in visitors. Even without this change the code gives 4 views instead of 1.

    • http://wpsnipp.com Kevin Chard

      Huh should only count by one, since its just count++ did you have any other people visit your site while it was running or multiple tabs open ?

      • Gabriel Merovingi

        Its on my localhost. Its really odd but might be just some browser thing. Thanks for a nice little snippet though!

      • Rgsanchezz

        Kevin,
        First off,  love the simplicity of this solution! I have the code working properly on my local server but noticed that when I reload my homepage (which contains my 5 most recent entries)  each reload of my homepage ads a view to the count for each of the 5 posts.  Is there any way to avoid that and only count the actual article page views? Thanks

        • http://wpsnipp.com Kevin Chard

          Hi Rgsanchezz glad to hear you like the snippet. In regards to your question make sure you place setPostViews(get_the_ID()); within the single.php template or inside if(is_singular()) should also work if you don’t use a single.php template that should resolve the problem.

    • Matteo

      Did you use this snippet in a separate loop? If so, you should place the setPostViews(get_the_ID()); outside the “most viewed” loop so that it counts the +1 only for the currently displayed post and not everyone in the list. For Instance:

      WRONG:

             
             
              ( )        CORRECT:                                ( )       

  • http://themet.metrostudentmedia.com/ Drew Jaynes

    Thanks for the snippet. I slightly modified setPostViews() to discount post views from Editors or above:

    function setPostViews($postID) {
    if (!current_user_can('level_7') ) :
        $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);
        }
    endif;
    }

    • http://wpsnipp.com Kevin Chard

      Thanks drew very cool.

    • Anna Bitanga

      Not working for me on localhost. Not sure why but every view, regardless if it’s an Editor level or above, gets counted.

      On another note, how would you disable counting the view by the post’s author?

      • Anna Bitanga

        Oops. Wasn’t logged in as admin during testing. (facepalm)

        • http://wpsnipp.com Kevin Chard

          Glad to hear you got things working.

  • http://www.facebook.com/people/Joj-Ala/100002337764441 Joj Ala

    how to remove the  “Views” word ?                       

    • http://wpsnipp.com Kevin Chard

      Hi Joj,
      You will see on line 09    return $count.’ Views';
      replace that line with the bellow and that is it.
      return $count;

  • http://www.InfoGenra.com Raj

    what is the impact of using this function over the WordPress database.. will I be able to clear of the logs (views) at set schedules.. ??

    • http://wpsnipp.com Kevin Chard

      This is not a plugin with a lot of features simply this will increment the count in the post meta by one each time the post is viewed.

  • Anonymous

    Example of Bob is possible?

    Most viewed posts
    Post title3 100 views
    Post title1 50views
    Post title2 10 views

  • http://www.thednetworks.com Dhawal D

    awesome stuff exactly what I was looking for?

  • Armand

    Any idea of how to use this with a Custom Post Type?

  • http://ZaidPirwani.com Zaid Pirwani

    Thanks, I am using it on a NEW site and it is working brilliantly….

    I am learning PHP on the GO, so your code taught me a couple of things.. THANKS AGAIN…

    • http://wpsnipp.com Kevin Chard

      Cool Zaid, glad to hear you like the site and hope that you enjoy the snippets!

  • http://www.cikletoyun.com/ Byciklet

    how to remove the  “Views” word ?

    • http://twitter.com/CRMWebDesign Christopher McMahon

      Edit Line 9

  • Anonymous

    How can we display the post views only in the admin page where all posts are displayed. I mean the edit.php page

  • Ituplus Dot Com

    Hello 
    Kevin Chard , i come from Viet Nam, my english is not good :(
    I want to ask you one question :) : how to display post view for only admin (only admin can see the post view ) :D
    You can send the code via my email :D

    • http://wpsnipp.com Kevin Chard

       this snippet will display the post views within the post column within the admin. However if you want to display within the site but only for admins you can just do this.

      if(is_admin()){
            echo getPostViews(get_the_ID());
      }

      • Stemie

        Kevin how can I configure it so that it only counts visitors who aren’t admin? This is not a display issue but a counting one. Im sure !is_admin() comes into it but not sure where? Thanks for great snippets btw.

        • http://wpsnipp.com Kevin Chard

          This should work for you, just replace step one code with the following.
           
          if(!current_user_can(‘administrator’)){
                echo setPostViews(get_the_ID());
          }

  • Hugo

    Hi Kevin – Great stuff ;) Quick question: Its works smooth for me but the counting starts from 0. Is possible to retrieve the views that I already had on the posts? thx

    • http://wpsnipp.com Kevin Chard

       Hi Hugo,
      Well the snippet saves to post meta, so if you had removed the snippet then added it again should continue from last values. However you can always edit the custom fields to change the values.

  • Mathieu

    Hi everybody !!

    Great tips ! Thank you very much.

    I was wondering if we can add a “time” data ?
    Because I would like to show the view of yesterday/ 2 days ago / 2 days ago etc… ?

  • http://twitter.com/acetheme AceTheme.com

    it works on chrome, firefox, but in safari, when refresh page the view number increase by 2, it there a fix, does anyone tested on safari

    • http://twitter.com/acetheme AceTheme.com

      find the problem, a line of jquery code makes it count twice when refresh, no idea why, but fixed it.

      • Kicker

        Hello, I have the same problem. Can you help me how did u fix it please? For me it counts twice in all of browsers

  • Bhaskar Relan

    Thanks a lot dear…A great piece of work….

    • Bhaskar Relan

       Yesterday I said it works…but somehow when we click on post it calculates fine but when we go to second newest post and then go to newest post the count for newest post is incremented by 2. Similar pattern can be seen with three.

      In case of three posts if we go to third newest post it will be incremented by 1 but when we go to second newest post it gets incremented by 2 and then go to newest post it is also incremented by 2.

      It we great if anyone can help on this issue.

      • GraceWang

        me too, every post is incremented by 2. why???

  • http://www.youamuseme.com Emma Larkins

    Awesome! Thanks for the code, seems to be working great.

  • Rafa

    Kevin,
    Can this script be used for custom post types?

    • http://wpsnipp.com Kevin Chard

       Hi Rafa, should be able to use this without any problems.

    • Rafa

       Ok, I will give it a shot.  How do we get the view counts to display on the CPT admin post columns?

      Thanks!

      • Rafa

        Figured out my issue, I had to add “post_type=any” to the query and I now see all my custom post types.

  • http://www.facebook.com/shady.rasmy Shady M Rasmy

    awesome work
    can i get code 
    to show the most viewed posts in widget or page 
     

  • Gio

    thank you man for your work.. it’s great

  • Pingback: WordPress Users: How to Increase Functionality AND Speed Up Your Site - ManageWP

  • http://twitter.com/justinwhall Justin W Hall

    hmmmmmm, how can I decrease by one after, say 24 hours ir incrementing?

    • http://wpsnipp.com Kevin Chard

      Let me just see if I understand correctly you want to minus one from each view every 24 hours ?

      • http://twitter.com/justinwhall Justin W Hall

        Yep, for each hit, subtract a hit 24 hours later. Get it? Ended up using a cron job. If you have an easier solution. I’m all ears.

        • http://wpsnipp.com Kevin Chard

          a cron is about the best way, another way would rely on people viewing a page to decrement and could easily get way behind.

  • GraceWang

    when i create a new tab in firefox, and it increments by 1, but i refresh the page, it increments by 2.
    Anyone has the same problem? why it happens? becauseof browser?

  • http://www.facebook.com/jeremyschultz1 Jeremy Schultz

    This looks like it works but I see an issue in my particular case…I am using this code within a sidebar widget (adding read count to a latest posts query):

    $args = array( ‘numberposts’ => 3, ‘order’=> ‘ASC’, ‘orderby’ => ‘title’ );
    $postslist = get_posts( $args );
    foreach ($postslist as $post) : setup_postdata($post);
    setPostViews(get_the_ID()); ?>

    I see two things:

    – The three posts listed always show an incremental count compared to one another (post 1 = 0 views, 2 = 1 view, 3 = 2 views). I’m not sure that is accurate.
    – When I refresh the page, these all have 3 added to the count (0, 1, and 2 now show 3, 4 and 5). I did add the remove_action code to functions.php but it looks like it had no effect.

    Not sure what could be the cause for this….

  • Jatin Soni

    I have tried this code it is inserting count but displaying same count for all post even I have added

    remove_action( ‘wp_head’, ‘adjacent_posts_rel_link_wp_head’, 10, 0);
    Any idea?

  • Anthony

    Great snippet. Works awesome.

    Do you know if it is possible to now have a “Most Popular Categories” list, based on the categories of the most viewed posts?

  • m8ke

    Awesome tips ! Thanks for sharing. I’d need a little more help though.
    I would like to display the most popular posts for only the last week or month ? Anybody can help ? Thx

  • http://www.moonfiller.com/ Taran

    Awesome tips ! Thanks a lot.

  • adrianbienias

    it counts only unique views, how to change it to counts every single view (every refresh)

  • http://tambnguyen.com/ Tam Nguyen Photography

    I’ve found that this code snippet doesn’t match with what I see with my Jetpack stats. Anybody else having the same problem?

  • QADiscussions

    Hey thanks man! But i dont get the image with views as you show here. Can you help me on this to get the image also.

  • Dipak

    I added the remove_action() , but still it adds 1 count to the recent post when i open any of the post. Please help .

  • http://tct.info.vn/ Hàng xách tay

    thanks

  • Anthony

    Is it possible to reset this counter? So lets say I want to display my most “popular posts of the week”. Every week the post views resets to 0.

  • Mia Autumn

    Thanks for the great snippet Man. is there anyway to accumulate all the previous visits to my every single post with the current post view count?

  • ali

    tanks

  • futeng

    good to know ,3q….

  • Dreamer

    This awesome thank a million

  • Jolle Jolles

    Great this works perfectly!

  • http://www.tips4blog.com/ Md. Iftekharul Ibna Alam

    You ordered the code snippet very nicely, and easy to understand. Now, I have posts view count in my blog. Thanks

  • Fred Surly

    This is perfect. Exactly what I was looking for.

  • Xavier

    If I have a custom field called views and exceeds 100 views, how I can show certain message in a template?

  • Daniel Ambrosio

    Friend how to do it also work on top the most visited post with this code below in your scripts ?

  • Sugawa

    Is it possible to change the ‘trigger’ of increment? for example, make certain links inside the post to increase the post-meta value whenever it’s being clicked. With this, maybe we can use this cool snippet for other uses. (ex: how many times our download link has been clicked)

  • Pingback: WordPress Users: How to Increase Functionality AND Speed Up Your Site

  • naveenbos

    Is it possible to get the profile viewers count with this code?

  • naveenbos

    Is it possible to get the profile viewers count with this code?

  • http://drhariri.com/ Abdulrahman Hariri

    Excellent code, thanks Kevin. I was wondering if you have tried to use this in author profiles? I am thinking of trying to use another version of this code to store values into author’s meta and count their profile page visits.

    Thanks!

  • http://drhariri.com/ Abdulrahman Hariri

    Excellent code, thanks Kevin. I was wondering if you have tried to use this in author profiles? I am thinking of trying to use another version of this code to store values into author’s meta and count their profile page visits.

    Thanks!

    • http://wpsnipp.com Kevin Chard

      Hi Abdulrahman Hariri,
      No I have not tried this in the author profiles myself however I can’t see any reason why it would not work. Other than updating the user meta rather than post meta should work well.

      • http://drhariri.com/ Abdulrahman Hariri

        I’ve tried it and its working :). Thanks a lot for the information!

  • Yves Jeanrenaud

    thanks for the code. I updated it to also work with caching and not using wordpress functions but directly altering the post_meta table. is anyone interested in these updates?

    • http://wpsnipp.com Kevin Chard

      Hi Yves Jeanrenaud I think many people on wpsnipp would love it. If you send the snippet along with some details, domain etc. That way we can give you credit that would be great.

      http://wpsnipp.com/index.php/contribute/

      • Yves Jeanrenaud

        ok, will do :)

    • Hector Castilla

      hi Yves, can you share your code? this snippet seem don’t work ok, as Tam ‘Heisenberg’ Nguyen said, my statistics don’t match too.

    • Hector Castilla

      hi Yves, can you share your code? this snippet seem don’t work ok, as Tam ‘Heisenberg’ Nguyen said, my statistics don’t match too.

    • Hector Castilla

      hi Yves, can you share your code? this snippet seem don’t work ok, as Tam ‘Heisenberg’ Nguyen said, my statistics don’t match too.

    • Egon Kilin

      Hi can you pls share the code here. I use WP Super Cache and i have problem with the counter.

      • Yves Jeanrenaud

        I did

        • http://wpsnipp.com Kevin Chard

          Hi Yves,
          I don’t think I ever got the code from you, send me an email on contribute and ill contact you via gmail so you can send it again if you don’t mind I think the code may have been the problem when sending it via the email form.

          • Yves Jeanrenaud

            oh really? my bad. I’ll send it again.

  • http://www.tips4blog.com/ Md. Iftekharul Ibna Alam

    Its conflict with w3 total cache, counts are increasing automatically in my blog. If anyone visits any of my post, the counts of rest of the posts are also increasing. Is there any solutions for this type of confliction?

    • http://wpsnipp.com Kevin Chard

      If you look at the first snippet at the bottom we have the following as

      prefetching with some browsers can add views to posts not viewed.

      // Remove issues with prefetching adding extra views
      remove_action( ‘wp_head’, ‘adjacent_posts_rel_link_wp_head’, 10, 0);

      The other thing to keep in mind that this snippet is not setup to play nice with cache plugins
      So I can’t be sure within much more testing how well this will work.

    • http://wpsnipp.com Kevin Chard

      If you look at the first snippet at the bottom we have the following as

      prefetching with some browsers can add views to posts not viewed.

      // Remove issues with prefetching adding extra views
      remove_action( ‘wp_head’, ‘adjacent_posts_rel_link_wp_head’, 10, 0);

      The other thing to keep in mind that this snippet is not setup to play nice with cache plugins
      So I can’t be sure within much more testing how well this will work.

    • http://wpsnipp.com Kevin Chard

      If you look at the first snippet at the bottom we have the following as

      prefetching with some browsers can add views to posts not viewed.

      // Remove issues with prefetching adding extra views
      remove_action( ‘wp_head’, ‘adjacent_posts_rel_link_wp_head’, 10, 0);

      The other thing to keep in mind that this snippet is not setup to play nice with cache plugins
      So I can’t be sure within much more testing how well this will work.

  • Randy

    Would you happen to help me in styling the echo output. I want to put the views count on my loop and I accomplished that perfectly but how do I add a css to the output for the text? Any ideas on that. Thank You

  • Randy

    Would you happen to help me in styling the echo output. I want to put the views count on my loop and I accomplished that perfectly but how do I add a css to the output for the text? Any ideas on that. Thank You

  • Randy

    Would you happen to help me in styling the echo output. I want to put the views count on my loop and I accomplished that perfectly but how do I add a css to the output for the text? Any ideas on that. Thank You

  • Randy

    Would you happen to help me in styling the echo output. I want to put the views count on my loop and I accomplished that perfectly but how do I add a css to the output for the text? Any ideas on that. Thank You

  • Randy

    Would you happen to help me in styling the echo output. I want to put the views count on my loop and I accomplished that perfectly but how do I add a css to the output for the text? Any ideas on that. Thank You

  • Randy

    Would you happen to help me in styling the echo output. I want to put the views count on my loop and I accomplished that perfectly but how do I add a css to the output for the text? Any ideas on that. Thank You

  • Randy

    Would you happen to help me in styling the echo output. I want to put the views count on my loop and I accomplished that perfectly but how do I add a css to the output for the text? Any ideas on that. Thank You

  • Randy

    Would you happen to help me in styling the echo output. I want to put the views count on my loop and I accomplished that perfectly but how do I add a css to the output for the text? Any ideas on that. Thank You

  • Randy

    Would you happen to help me in styling the echo output. I want to put the views count on my loop and I accomplished that perfectly but how do I add a css to the output for the text? Any ideas on that. Thank You

  • Randy

    Would you happen to help me in styling the echo output. I want to put the views count on my loop and I accomplished that perfectly but how do I add a css to the output for the text? Any ideas on that. Thank You

  • Randy

    Would you happen to help me in styling the echo output. I want to put the views count on my loop and I accomplished that perfectly but how do I add a css to the output for the text? Any ideas on that. Thank You

  • Randy

    Would you happen to help me in styling the echo output. I want to put the views count on my loop and I accomplished that perfectly but how do I add a css to the output for the text? Any ideas on that. Thank You

  • Randy

    Would you happen to help me in styling the echo output. I want to put the views count on my loop and I accomplished that perfectly but how do I add a css to the output for the text? Any ideas on that. Thank You

  • Randy

    Would you happen to help me in styling the echo output. I want to put the views count on my loop and I accomplished that perfectly but how do I add a css to the output for the text? Any ideas on that. Thank You

  • Randy

    Would you happen to help me in styling the echo output. I want to put the views count on my loop and I accomplished that perfectly but how do I add a css to the output for the text? Any ideas on that. Thank You

    • http://wpsnipp.com Kevin Chard

      Hi Randy you can do the following by adding a div around the php function. i added a sample on pastebin
      http://pastebin.com/ThLasSJB

      • Randy

        I tried that right now and its not working.

      • Randy

        I tried that right now and its not working.

      • Randy

        I tried that right now and its not working.

      • Randy

        I tried that right now and its not working.

      • Randy

        I tried that right now and its not working.

      • Randy

        I tried that right now and its not working.

        • http://wpsnipp.com Kevin Chard

          Hi Randy,
          is the snippet displaying the number of views? if so it has something to do with your css or html as copying that snippet and placing it within your single post loop should work fine. You just have to define some styles to apply.

  • Junio

    Hello!
    Sorry for bad english, I’m using a translator. I’m from Brazil.
    I have a site that already has several views on the post, as I do not lose these views? Is to update the database? I use this plugin: WP-PostViews

  • Chris

    Great snippet but still incrementing on every post.

  • Renatogarou

    Wow. This saved me time. Very nice dude thank you.

  • Tai

    Hi, this seems to stop working in WP 4.0. Can anyone confirm or provide fix? thx u!

    • Scott

      Have you heard anything on this? I just upgraded to 4.0, and would like to use this.

    • José

      Same here.

    • Liem Le

      I’m using 4.0 and nothing wrong :)

  • Andrew

    for me it is counting twice…

    • http://wpsnipp.com Kevin Chard

      Hi Andrew, is likely prefetching, or the setViews is added more than once. To solve the one issue try adding this. Note the code above.

      // Remove issues with prefetching adding extra views
      remove_action( ‘wp_head’, ‘adjacent_posts_rel_link_wp_head’, 10, 0);

  • http://justpress.me/ Andy Shah

    Where is the count stored? Is it in a database?
    Wanted to know if it’s possible to change the values.

    Works well for me on latest version of wordpress, thanks.