Support » Fixing WordPress » error creating visit counter

  • jemarquesini


    Hi all!

    I’m trying to make a simple visit counter: i add a ‘post_hits’ field in the wp_posts table (like PostCount plugin does) and every time you visit a post, it increases that field in 1. Simple, isn’t it?

    Not at all.

    To do it, i tried this: in single.php, within the loop, i put this code line:

    mysql_query("UPDATE wp_posts SET post_hits=(post_hits+1) WHERE ID=".get_the_ID(),$link)

    It should works. In fact, it does, but more than i expected… If i visit a post, it records the visit in that post, but also records it in the next post !!!

    I mean, if i visit post 90, single.php records the visit in the ID=90, and in the ID=91 as well.

    To solve it, i took that code line out of the loop, and it kept on working that way.

    So i changed the idea, and started using meta-data. ‘Well, this time will work’, i thought. BUT, it keeps recording the information in each post i visit, and next one.

    I’ve been looking for some information, in some forums, and some people have tried this, but they get the same problems than me with some themes, but not with others. E.g., some people says Twenty Ten works fine, but others fail. Mine is Angel.

    Any idea?

Viewing 1 replies (of 1 total)
  • Ganners


    It could be that your single page has a next button to go to the next post and perhaps the ID for that is being pulled and executed, but I’d have to see much more of the code to try and work it out.

Viewing 1 replies (of 1 total)
  • The topic ‘error creating visit counter’ is closed to new replies.