in Razno, WordPress Plugins

CountPosts v 1.0 – WordPress Plugin [en]

Latest Version: CountPost 2.0
CountPosts V 2.0 a for latest WordPress versions from 2.5 to 2.6.2
Now it don’t count post from all users with user-level 8, (admin permissions), so it will not count visits from whole admin team, not just one admin person like older version

CountPosts V 1.1 Beta for WP 2.0

CountPosts is plugin for WordPress Blog. It’s very easy to install and use.

This plugin count visits on your blog, but not when u are log-in as admin. Also count visits for every post separated, and u know in every moment how many people read your posts. U can see what is the most read post in your blog. Also when u read your posts, CountPost won’t count your visits.

CountPost make u list of the most read post, with hits for every post separated.


1) Add CountPost.php in Plugin folder
2) In Plugins section, click on the activate button for CountPost plugin
3) Add

  1. span class=”st0″>’Hits for this post:’,

in index.php or single.php (It depending of your Theme you using) somewhere before comments or where ever you like. First and second attribute can be changed.
4) Add

  1. span class=”st0″>’Visits: : ‘, ‘ .’

in sidebar.php First attribute is print before visits number, and second is after visits number
5) Add


somewhere when u want to show top list of read posts.

Using TopHitsList(); function

  1. span class=”st0″>’ (‘, $afterRead = ‘ ).’‘<ul>’‘<li>’‘</li>’);
  1. span class=”st0″>’ (‘, $afterRead = ‘ ).’‘<ul>’‘<li>’‘</li>’);
  4. /*$beforeAll – print before all.
  5. $afterAll – print after all.
  6. $beforeLink – print before post link.
  7. $beforeRead – print after post link and before hits.
  8. $afterRead – print after hits.
  9. $afterLink – print after $afterRead*/

Sample of using TopHitsList() function

  1. span class=”co1″>//in unnumbered list
  2. ‘ Read  ‘,‘  times.’, $top = 5,  ‘<ul>’, ‘<li>’, ‘</li>’, ‘</ul>’);
  4. //in numbered list
  5. ‘ Read  ‘,‘  times.’, $top = 5,  ‘<ol>’, ‘<li>’, ‘</li>’, ‘</ol>’

Output will be something like this:
POST NAME1 Read 12 times.
POST NAME2 Read 34 times.

Older version problems:

  • Version 1.0 don’t work if your blog use Revrite Rule. Now it works even if your link structure looks like this :
  • When activate some other plugin, you had error: WordPress database error: [Duplicate column name ‘post_hits’] . This error wasn’t making any problems, but it was annoying.

Download Latest version:
Download CountPost for WordPress 1.5

Download CountPost for WordPress 2.0

Update 5. April 2006
New version 1.1 Beta is hire. I fixed some bugs in 1.0 version:

If u have any problems, suggestions for this plugin contact me.

Write a Comment



  1. Hello!

    The plugin seems really nice, but i am also having some trouble with installation. The SQL tables aren’t created automaticly when i activate the plugin … could you send me a text file with the SQL so i can insert them manually?


    My email is : [email protected]

  2. SQL1 = “ALTER TABLE wp_posts ADD post_hits int(11) default ‘0’ NOT NULL”;
    SQL2 = “CREATE TABLE wp_mainCount (main_hits int(11) default ‘0’ NOT NULL)”;
    SQL3 = “INSERT INTO wp_mainCount VALUES (0)” ;

    wp_ is prefiks for my WordPress database if your prefiks is diferent change wp_ with your prefiks in SQLs.

    I am not sure why tables arn’t been created. If u have more problems contact me.

  3. It doesn’t work…

    Hits for this post:’, ”); ?>
    in index.php somewhere befor coments

    Could you be must precis ?
    I test to drop in different way, and the same for

    and now, there is a big bug ! :-/
    thx for your help !

  4. Yop,
    See the error :
    your code don’t mark well the $p of the post !
    I try with $_GET[“p”];
    Doesn’t work too…

    grrrr !
    //if ($p!=”)
    will never work if $p doesn’t exist…

    help !

  5. [code]
    < ?php $p = $_GET['p']; HitThisPost($p,'Read ', ' times'); ?>

    Or u can try with
    < ?php // $_GET don;t need hire becouse $post is global variable HitThisPost($post->ID,’Read ‘, ‘ times’);

    First exsample work, but only when u read whole post. Then in you browser address is something like this

    Put this code after
    < ?php if ( pings_open() ) : ?>
    ” rel=”trackback”>< ?php _e('TrackBack'); ?>
    < ?php endif; ?>

    Where do u put this code depends of your Theme.
    Tell me did i helped u…

  6. Is it possible to make an bugfixed version which is compatible to teh newest WP`? Cos i have also all this errors and i cant fix them 🙁
    To bad it seemed a good plugin

  7. for pasteler0:
    Hits can see everybody, visitors and admin, but hits are counting only when visitors read posts, and not when admin read post. Understand ?

  8. I got this error:

    WordPress database error: [Duplicate column name ‘post_hits’]
    ALTER TABLE wp_posts ADD post_hits int(11) default ‘0’ NOT NULL

    How can I fix these problem?
    [I just deactivate these plugin, activate again, and these warning appear]

  9. Do not make attention on this error. This error can’t make any problems. I try to fix this in next plugin version.

  10. This seems like a really nice idea. I installed it and put the code where you say. The overall visit count for the site seems to be updating by the second, but the individual post counts are not updating at all.

    In case you are wondering I am using the latest WP2 and Firefox. I will be disabling and removing the code.

  11. It’s working already for me! 100%! I was logged as Admin earlier that’s why I can’t see any hits incrementation. Very clever plugin! Keep up the good work!

  12. Hi:
    Off topic, but I visited here for the purpose of showing number of posts in each category. And you have it in your each category: blog world (26). This “(26)” counting function is something I am looking for. Would you kindly tell me what plugin it is? And were to get it? Thanks! Sunny

  13. Hi Ivan,
    I’d like to view, in my archive pages, the visits for any post.
    With the HitThisPost function i’ve some troubles, because if i refresh the page i’ve an increment of 1 in every post shown in archive.
    How can i bypass this problem?


  14. Stefano,
    I need to know what WP do you have, 1.5 or 2.0 ?

    If you are log-in as admin you wouldn’t increment post views, but if you are not log-in then you increment post views just like every regular visitors.

  15. Ivan,
    I’ve WP ver. 2.0.2, I know there’s no increment in post if I’m logged as admin, but – for example – in my archive page i see 20 posts per page, with the number of visit for every post.

    If I’m not logged and refresh the archive page i’ve an increment in ALL posts shown in archive…at the same time a malicious user can refresh the page in order to increment all the counters, and it’s not good.

    I would like to increment counter ONLY in single.php page and not in archive pages that shown visitor’s number.

    Thx a lot 😉

  16. hi, I am using the top ten hottest function for my website. however, how to have an ‘Last ten coolest articles’ page? Can you teach me? Thank you so much!

  17. Dear Mr Ivan, the download link for the plugin (2.0) seem to be broken. Iäm getting 404 error over here in Poland.

    Could you please send me an email when its up again?

    Thx in advance.

  18. Hi Ivan, I´d love to get a “Most Read” into my blog. But your links does not work for me too. Could you send it to me, please?

  19. Hello to all ya 🙂

    Ivan, big thanks for your plugin.
    Stefano, You have such problem, ’cause this plugin is only for counting hits and displaying Top List.
    If You want ONLY DISPLAY (not count) hits, You have to add in the CountPosts.php such code:

    function PostHits($beforeRead = ‘ (‘, $afterRead = ‘ ).’) {
    global $wpdb, $table_prefix,$wp_query;
    $PostID = $wp_query->post->ID;
    $SQL = “SELECT ID,post_hits FROM ” . $table_prefix .”posts WHERE ID = ‘”. $PostID.”‘”;
    $hits = $wpdb->get_results($SQL);
    foreach ($hits as $hit) {
    echo $beforeRead;
    echo $hit->post_hits;
    echo $afterRead;

    and add for example

    in place, where You want to display hits count (for me it was archive.php, search.php, index.php).

    And You have to add string

    Hits for this post:’, ”); ?>

    ONLY in single.php to count hits.

  20. something strange with comments and crazy captcha :))

    Ivan, big thanks for your plugin.
    Stefano, You have such problem, ’cause this plugin is only for counting hits and displaying Top List.
    If You want ONLY DISPLAY (not count) hits, You have to add in the CountPosts.php such code:

    function PostHits($beforeRead = ‘ (‘, $afterRead = ‘ ).’) {
    global $wpdb, $table_prefix,$wp_query;
    $PostID = $wp_query->post->ID;
    $SQL = “SELECT ID,post_hits FROM ” . $table_prefix .”posts WHERE ID = ‘”. $PostID.”‘”;
    $hits = $wpdb->get_results($SQL);
    foreach ($hits as $hit) {
    echo $beforeRead;
    echo $hit->post_hits;
    echo $afterRead;

    and add for example in place, where You want to display hits count (for me it was archive.php, search.php, index.php).

    And You have to add string Hits for this post:’, ”); ?> ONLY in single.php to count hits.

  21. and again 🙁
    Stefano, You have to add for example

    PostHits(‘Views:’, ”);

    in place, where You want to display hits count (for me it was archive.php, search.php, index.php).

    And You have to add string

    HitThisPost(‘Hits for this post:’, ”);

    ONLY in single.php to count hits.

  22. Hi Ivan – I’ve used your plugin for a little while but then decided I wanted to have a count of the most commented articles on my site.

    So I simply added to your plugin the following code:

    function TopCommentsList($beforeRead = ‘ (‘, $afterRead = ‘)’, $top = 5,
    $beforeAll = ”, $beforeLink =”,
    $afterLink = ”, $afterAll = ”) {
    global $wpdb, $table_prefix;
    $SQL = “select A.ID, A.post_title, (select count(*) from ” . $table_prefix . “comments where comment_post_ID = A.ID and comment_approved = ‘1’) as comments_count, A.guid from ” . $table_prefix .
    “posts A order by 3 desc LIMIT 0,” . $top;
    echo $beforeAll;
    $tops = $wpdb->get_results($SQL);
    foreach ($tops as $top) {
    echo $beforeLink , “guid , “”>” , $top->post_title , $beforeRead , $top->comments_count , $afterRead , ““, $afterLink;
    echo $afterAll;
    echo “Comments Counter Powered by“;

    I’m not really interested in making it available as a plug-in in its own right, but if you want to include it in yours I’ll be happy – especially if you credit me and/or leave the link in :o)



  23. Hi Ivan,

    I would like to use your plugin, but I am not comfortable with changing the table “posts”. It might backfire in a future WordPress upgrade.
    I would prefer to have a dedicated table for the statistics. Do you think this could be done?



  24. Hi Mac,

    Yes it is possible. You can change my plugin. Just create table with post ID and count attributes.

    If you create new plugin, please inform me.

  25. For Bogdan:

    You must put the count function in the
    loop, in the single.php file in your template folder. If the function is not in the loop, it don’t work.

  26. Hello,
    Nice plugin.
    I’ve changed it a but though…
    I think that doing the read and write in the same function is not so good.
    Let me explain you. I want to show in my blog how many time a post has been read but when you’re in the list not in the post itself.
    So based on your code I’ve done a ReadCounter and WriteCounter. the readCounter display only how many times a post has been read ( so it can be include in your index.php ) and the WriteCounter add +1 to the counter. This call is made in the single.php.

    here is the CountPost.php
    get_var($sql) == null) {
    $alter = “ALTER TABLE ” . $table_prefix . “posts ADD post_hits int(11) default ‘0’ NOT NULL”;
    $create = “CREATE TABLE ” . $table_prefix . “mainCount (main_hits int(11) default ‘0’ NOT NULL)”;
    require_once(ABSPATH . ‘wp-admin/upgrade-functions.php’);

    $Insert = “INSERT INTO ” . $table_prefix . “mainCount VALUES (0)” ;

    function WriteCounter() {
    global $wpdb, $table_prefix, $wp_query, $current_user;
    $p = $wp_query->post->ID;
    if ($p)
    $Update = “Update ” . $table_prefix . “posts set post_hits = post_hits + 1
    WHERE ID=” . $p ;
    //echo $current_user->wp_capabilities;
    //if (get_profile(‘user_login’) != ‘admin’)
    if(!current_user_can(‘level_2’) )

    function ReadCounter( $before = ”, $after=” ) {
    global $wpdb, $table_prefix, $wp_query, $current_user;
    $p = $wp_query->post->ID;
    if ($p)
    $SQL = “SELECT post_hits FROM ” . $table_prefix . “posts WHERE ID=” . $p;
    $red = $wpdb->get_row($SQL);
    echo $before . $red->post_hits . $after;

    function TopHitsList($beforeRead = ‘ (‘, $afterRead = ‘ ).’, $top = 5,
    $beforeAll = ”, $beforeLink =”,
    $afterLink = ”, $afterAll = ”) {
    global $wpdb, $table_prefix;
    $SQL = “SELECT ID, post_title, post_hits, guid FROM ” . $table_prefix .
    “posts ORDER BY post_hits DESC LIMIT 0, ” . $top;
    echo $beforeAll;
    $tops = $wpdb->get_results($SQL);
    foreach ($tops as $top) {
    echo $beforeLink , “guid , “”>” , $top->post_title , $beforeRead , $top->post_hits , $afterRead , ““, $afterLink;
    echo $afterAll;
    echo “Top List Powered by Count Post Plugin“;


    function mainCounter($before = ‘Visitors: ‘, $after = ‘.’) {
    global $wpdb, $table_prefix;
    $Update = “Update ” . $table_prefix . “mainCount set main_hits = main_hits + 1”;
    if( !current_user_can(‘level_2’) )
    $SQL = “SELECT main_hits FROM ” . $table_prefix . “mainCount”;
    $red = $wpdb->get_row($SQL);
    echo $before , $red->main_hits , $after;
    echo “Counter Powered by Count Post Plugin“;

    if (isset($_GET[‘activate’]) && $_GET[‘activate’] == ‘true’) {
    add_action(‘init’, ‘InstallCountPostPlugin’);

    here is the index.php

    ” rel=”bookmark” title=”Permanent link to “>



    posted in

    Not found!
    Sorry, but you are looking for something that isn’t

    and the single.php

    ” rel=”bookmark” title=”Permanent link: “>

    Posted in



    Pages: ‘, ”, ‘number’); ?>

    comment_status) && (‘open’ == $post->ping_status)) {
    // Both Comments and Pings are open ?>
    You can leave a response, or ” rel=”trackback”>trackback
    from your own site.
    comment_status) && (‘open’ == $post->ping_status)) {
    // Only Pings are Open ?>
    Responses are currently closed, but you can ” rel=”trackback”>trackback
    from your own site.
    comment_status) && !(‘open’ == $post->ping_status)) {
    // Comments are open, Pings are not ?>
    You can skip to the end and leave a response. Pinging is currently not allowed.
    comment_status) && !(‘open’ == $post->ping_status)) {
    // Neither Comments, nor Pings are open ?>
    Both comments and pings are currently closed.

    Sorry, no posts matched your criteria!

    Hope it will helps some..

    Thx anyway one more time for this plugin 😉

  27. For some posts which I’ve modified the title, the link pointed by the TopHitLists is not correct. I see that you are using the guid field to make the link reference, can you change to get_permalink function?
    Besides, how to remove the database table installed by your plugins?

  28. Robert,

    You can remove table with drop table in phpMyAdmin.
    And yes get_permalink function is bettet solution. I will change it.

  29. Hi Ivan,

    I have kind of the same problem as Stefano. Using wp 2.0.4, every time the page is refreshed, the hit counter for all the items is incremented.
    I only want it to increment when someone hits: read more

    Can you help me out here?

  30. Roel,

    Off course, just add this function in your template.

    Set the $top variable at top articles number you wish.

    For top five articles use this code:

    ‘, ‘

  31. ‘, ‘
  32. ‘, ”); ?>

  33. Ellen,

    You must put function HitThisPost in single php file in the loop.

    If you put it in the index.php it will increment post hits on every visit, for all post on the index page.

    So you just need to move function to the single.php

  34. Ivan,

    thanks for your reply; I’ve used the code, but I would prefer not to show the number of times the article has been read. Right now I have:
    – article 1 (500)
    – article 2 (498)

    I would like to show:
    – article 1
    – article 2

    Would this be possible?

  35. I love your plugin it is exactly what I was looking for and it works wonderfully.

    I run a website for a non-profit organization, and would like to keep stats on which of our adoption listings are being veiwed most each week.

    I am going to look at this myself but I’m struggling a bit.

    Would you know a way to get it to clear the stats automatically at the end of each week so that I have fresh stats for the following week. Like say make it reset the stats on Saturday night at a certain time.

    If you could help me with the query that would accomplish this I could help create an admin page so that folks could turn the feature on or off. I’m just not good with date functions *blush*

  36. Dear,

    The plugin is great, but I have one small comment.
    Everytime The Loop goes around (10 posts on front page for example), every post on the front page gets its post_hits incremented. Thats not really what I though it would do, so i suggest the following code change:

    function HitThisPost($before = '', $after='', $detailed_view = true) {
    global $wpdb, $table_prefix, $wp_query, $current_user;
    $p = $wp_query->post->ID;
    if ($p)
    $Update = "Update " . $table_prefix . "posts set post_hits = post_hits + 1
    WHERE ID=" . $p ;
    //echo $current_user->wp_capabilities;
    //if (get_profile('user_login') != 'admin')
    if(!current_user_can('level_2') && $detailed_view)
    $SQL = "SELECT post_hits FROM " . $table_prefix . "posts WHERE ID=" . $p;
    $red = $wpdb->get_row($SQL);
    echo $before . $red->post_hits . $after;

    So on the index.php you use the third argument false and on the SinglePost.php (for example from Almost Spring), we use true as the third argument.

    Let me know if you think this is interesting.


  37. Hi – I also agree with SabbeRubbish in the above comment. This is a kewl plugin but I needed a way to reveal the count in the index.php of al the posts without having the count increase for each post! I hope you add the above fix in an official release. Thanks for the plugin!

  38. Great Plugin… I’ll sugest and option page for reseting post_id… I’ve changed the post-slug of a post after it was published, so the plugin is still pointing to the old post-slug… don’t know how to solve it. Greeting

  39. Soon i will write a new version of this plugin, and it will have more features like reseting counts for some post, and other from above comments…

    Thanks for your suggestions!

  40. i have this error

    Parse error: syntax error, unexpected T_FOR in /home/.monatana/todhirsch/ on line 18

    and in the line 18 i put


    please if someone know why, please letme know

    10nx everybody

  41. I’m getting the same error as erick after upgrading to Wp 2.1.3. what is the solution? is there an update for this plugin?

  42. Hello Ivan,
    It seems to be a great plug-in, i have several blogs using WordPress 2.2 and 2.1.3, and I´d like to use it, but it seems that there are some problems, is there any compatibility problem with recent versions? or do I have to wait for your upgrade?
    I see that you provide an excellent support to all of your fans, I would appreciate your comments.

  43. Hi there,

    first of all – thx for the great plugin. I am just puttin up my first blog. Installed CountPost under WP2.2.1 and it works great.

    BUT – I also installed the Language Switcher WP-Plugin ( today to create a switchable English/German blog. Everything still works, but the ‘TopHitsList()’-function (as you can see here: does not.

    I told the programmer of the LS-Plugin and this is her reply:

    “You will have to make sure that whenever the
    other plugins (in their PHP code) put information on the screen (such as post titles), that the plugins “filter” the information using the standard WordPress filters. If you are a PHP programmer, I can point you to some resources on the WordPress site to learn about filters, and you can fix the plugins. If you are not a PHP programmer, then your only recourse is to try to explain to the plugin authors that
    they should always use the standard WordPress filters when putting out information, and hope they fix it.”

    Your countPost Plugin is really very cool – so my question: Is there a chance, you make use standard WP filters in one of the next versions?

    Thx & Greetings,

  44. I know from my own experience that one only recieves bug reports and never just a pure, innocent “thank you” – but tonight I will change the game:

    Thanks for the plugin. I installed it and it just works fine!

  45. Great plugin!! I use it on my blog… only one problem… I’ve changed the permalink structure of the blog, but the CountPost database didn’t change so I’m receiving 404 error when clicking the link provided by CountPost plugin… any chance of a new version with Option Panel for rebuilding the permalink structure in CountPost database??

  46. I solved the problem of permalink change… you have to edit this awesome plugin and change the part where the SQL calls for “guid” field, for get_permalink() function…

    It should be like this

    echo $beforeLink , “” , $top->post_title , ““, $beforeRead , $top->post_hits , $afterRead , $afterLink;

    And problem solved… greetings

  47. WordPress: v2.3.2
    MySQL: v5.0.45-community-nt
    PHP: v4.4.7
    Server: Microsoft-IIS/6.0
    Firefox v2.0.0.11

    Plugin activates but throws error on page.


  48. Nope, doesn’t work on wp 2.6.2, chrometweaks template.
    Tryed to place it on index.php, single.php and page.php. The result is allways the same, as in index.php:

    «Parse error: syntax error, unexpected T_FOR in /home/deictorg/public_html/ on line 41»

    But maybe I’m being stupid. What I’ve made was simply paste de code as follows


    to the file, under the comments php.

    Is it necessary to do anything more?

    Would you be kind to send me an e-mail regarding this issue?
    I think it’s a great tool and it’s a pitty I can’t use it.


  49. Hey Ivan, thanks for the plugin. I was wondering if I could exclude a category from the output and vice versa. How can I get in touch? Thanks,


  50. Thanks you very much. Would you know a way to get it to clear the stats automatically at the end of each week so that I have fresh stats for the following week. Like say make it reset the stats on Saturday night at a certain time.


  • Wordpress Plugins « February 25, 2009

    […] CountPosts (  […]

  • - » WordPress 2.0.5 - Ronan February 25, 2009

    […] 4. CountPosts […]