• Hello,
    I’ve been looking around the wiki and forums but haven’t found anything about a counter indicating how many times a post has been read. This helps internally to later determine the most popular (read) posts, and also as a nice public stats feature. Is this what is meant by “Activity logging” (among other features) in the Future section of the site ?
    I’d appreciate any pointers if this already exists, maybe I’m too sleep deprived to notice it.
    Cheers,
    mf.

Viewing 15 replies - 1 through 15 (of 29 total)
  • I wrote a script which determines the popularity of a post (and outputs the most popular posts) if thats what you are looking for.
    Here is the link to the forum discussion:
    http://dinki.mine.nu/word/index.php?p=108&more=1&c=1

    Thread Starter _mf_

    (@_mf_)

    Not exactly. Here’s an example:
    http://www.lynnrockwell.com/weblog.php/
    Look at the end of each post, just before the “link me” permalink, it says “XX views”.
    I guess this is my lazyweb request of the day…

    That is a great idea! Ill see what I can come up with! πŸ™‚

    I have a solution. You can see how it looks at http://dinki.mine.nu/weblog/ and you can download the hack instructions at: http://dinki.mine.nu/word/index.php?p=108&more=1&c=1

    Thread Starter _mf_

    (@_mf_)

    Exactly what I wanted πŸ™‚ Cool… Why don’t you display it on your home page (I only see it in the comments/full post pages) ?

    Its on the home page. However, nothing is displayed when a post has not received any hits yet. πŸ™‚
    Glad you like it.
    Peace

    Running into an error:

    Warning: mysql_affected_rows(): A link to the server could not be established in /home/deanlog/public_html/blog/wp-includes/wp-db.php on line 173

    $this->rows_affected = mysql_affected_rows();
    I think it has something to do with the $wpdb call.
    Is there a way to close the $wpdb connection?
    I use it in another hack, so I could use this script the way it was created.
    My revised script:

    function add_count($p_number) {
    global $wpdb;
    $result = $wpdb->get_results("SELECT post_ID, postView_cnt FROM b2postView WHERE post_ID = $p_number");
    $test = 0;
    if($result){
    foreach ($result as $result){
    $newCount = $result->postView_cnt + 1;
    $postquery = ("update b2postView SET postView_cnt = $newCount WHERE post_ID = $result->post_ID");
    $results = $wpdb->query($postquery);
    $this->results = false;
    $test = 1;
    }
    }
    else{
    $postquery = ("insert INTO b2postView VALUES($p_number, 1)");
    $results = $wpdb->query($postquery);
    $this->results = false;
    }
    }

    Why are you calling $wpdb in the add_count function? You don’t need to.

    a few quick comments:
    – great idea! πŸ˜‰
    – it’d help if everyone would write hacks to use $wpdb at all times. I have to modify all hacks otherwise, as I have a lot of debug/performance tracking systems embedded in wpdb… It also means there’s only one open of the database occurring per page.
    -the addCount thing could use to do an atomic operation. Otherwise it’s possible two accesses will cancel each other out. I’m looking into this for other reasons, will report back when I figure it out.
    – my other mod to this will/would be to have it store the data as an extra column in the wp_posts table, rather than creating a new one. The advantage being that the new data will come implicitly in the post loop without a second query or a join or anything. πŸ˜‰ yeah, it means modifying the base structure of the table — but since I’ve already done that once… πŸ˜‰ πŸ™‚
    Some great work here — I’m looking forward to figuring out how to pull some data from my referral tracking db to ‘initialize’ my counts to something reasonable, then start it all up. Keep up the hacks guys!
    -d
    http://www.chait.net

    @david: I would like to clarify my reasons for not using $wpdb everywhere.
    1) Using mysql_query does not mean a new database connection. This hack, for example, uses the existing database pointer and does not open a new one
    2) $wpdb performs a bunch of extra data manipulation tasks that are not always required
    3) $wpdb does not have a lot of the built in MySql/php features implemented such as count_tables etc (thus a lot of the vanilla WordPress code still contains native MySql calls, as of 1.0 )
    However, I do agree that we should try to code within the confines of wpdb as far as possible to make the hacks follow the WP coding style guidelines.
    http://wordpress.org/docs/developer/#coding-style

    I’m with you — and of course agree all your reasons are extremely legit. some quick notes back at ya (since we help the populace learn by tossing out examples):
    for (1): I’ve seen cases where people were re-opening connections to the db — I noted that just for clarity, not that you were! πŸ˜‰
    for (2): there’s likely more overhead to the SQL lookup itself. also, as I mentioned I’ve got db debug stuff, which basically tracks every query call coming through wpdb including a string name of caller, good for tracking too-many-queries or not-enough-caching. πŸ™‚
    and for (3): all the more reason to enhance wpdb to expose needed functionality! πŸ˜‰ In addition to coding guidelines (some of which I don’t agree with, especially some of the formatting! πŸ˜‰ ), it also forwards the ability to remove dependence on MySQL in the future… πŸ™‚ Almost worth starting a new topic on enhancing wpdb for that reason alone…
    I just basically cloned your idea but embedded into the posts table. One update query does the +1, the count is already in $post when needed, and popular posts queries one db. But again, that’s great for someone like me who’s already hacked up the tables — for 99% of users out there, your approach wins hands down, no question… well, and I haven’t tested mine yet.
    Bitch that I didn’t have this in place two months ago — reconstructing counts from referrer log wasn’t overly useful, as it’s easily missing dozens or even hundreds of views… but gotta start somewhere!
    By the way Mark, want to reinforce again cool idea and sample! Keeps me thinking about methods for enhancing my site, and continues to push me to learn more SQL tricks. πŸ˜‰ Keep up the good work!
    -d
    http://www.chait.net

    I had to switch it because I was getting more errors the other way. This hack was breaking my wp-people hack.
    Now I only get the one error, I just want it to go away. Please let me know what you figure out.
    It only shows up when you click on the post to view it.

    @david: Dude, you are awesome! I really was not criticising you, but referring to some of the weaknesses of $wpdb I love your site, BTW Read it when I need some good tech-browsing material. πŸ™‚
    @logan: For my hack, try to modify it this way:
    Replace every occurance of $row, $result and $results, with $row123, $result123 and $results123 respectively. Let us know what transpires.

    Reverted add_count back to what you had.
    Added suggested changes.
    Still no luck.
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/deanlog/public_html/blog/my-hacks.php on line 14

    Quick and (really) dirty fix: Find the mysql_fetch_array() function in question and add an @ in front of it, like so:
    @mysql_fetch_array()

Viewing 15 replies - 1 through 15 (of 29 total)
  • The topic ‘REQ: Post read X times counter’ is closed to new replies.