The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

error creating visit counter (2 posts)

  1. jemarquesini
    Posted 5 years ago #

    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?

  2. Ganners
    Posted 5 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic