header

Top 10 Posts/Views

Thanks to a tip from podz, I installed Top 10 Posts/Views plugin. This gave me the functionality of showing view count for each post, and showing the 10 most viewed posts. I made a few changes to make it work right on my site.

First I followed the documentation, which was just fine, no problems there. Installing was easy and quick. Only changes I made were:

  1. In show_pop_posts: before the showeach block add <ul> and after it add </ul>
  2. In show_post_count: changed echo $before.$postcount.$after; to echo $before.(($postcount)-1).$after; (I was getting off-by-one)
  3. In show_pop_posts: changed $post_title = substr($post->post_title, 0, 20); to $post_title = $post->post_title; (I thought the first 20 chars of a post was rather ugly)

I also noticed an error when displaying with the default post numbers instead of permalinks, but I don’t use it, so I’m worried.

Other than that, it works exactly how I wanted. The number of queries can probably be reduced from 2*num posts shown + 1 for top10. I won’t worry about that because it doesn’t really slow me down.

Edit 6/24:
Added functionality so that if the user has already seen a post, it will only be logged (and incremented) the first time. I did this with the $_SESSION superglobal, using the following code. In add_count:

        $tmp = explode("&", $_SESSION["p"]);
        if(!in_array($p_number, $tmp)){
.
.
.
                $_SESSION["p"] .= “$p_number&”;
        }

IMO this is better than only logging if is_single() is set… i.e. if the user is only viewing that post, but it can be used in conjunction with that method for best results.

13 Comments

  1. gravatar

    Raybdbomb Said,

    June 2, 2005 @ 9:53 am

    I also propose another code enhancement. Currently the author of the code does a separate query for each “popular post”… he can condense it into one query using the following code:


    $results = $wpdb->get_results("SELECT m.postnumber, m.cntaccess, p.ID, p.post_title
    FROM mostAccessed m, $tableposts p
    WHERE m.postnumber = p.ID
    ORDER BY m.cntaccess DESC LIMIT 10");
    echo "<ul>";
    foreach ($results as $result) {
    $postnumber = $result->postnumber;
    $post_title = $result->post_title;
    $urlperma = get_permalink($result->ID);
    echo "<li><a href=\"$urlperma\">$post_title</a> <strong>($result->cntaccess)</strong></li>";
    }
    echo "</ul>";

    The result is 10 (or however many “most popular posts” you show) less. Forgive the bad formatting :P

  2. gravatar

    Jonathan Said,

    June 2, 2005 @ 8:58 pm

    Interesting rewrite. I didn’t originally write the code, LaughingLizard did. I just ported it over as plugin and documented it correctly. If he decides to do so, he or I will update the code.

    The plugin works just fine for post numbers if you read the plugin. Just comment out one of the lines and use a different code set inside the loop. Instead of $id its $p. Hope that helps. Thanks for your contribution.

  3. gravatar

    Jonathan Said,

    June 2, 2005 @ 9:00 pm

    A couple suggestions as well after I posted. One, change the comment auth to just using Spam Karma 2 && Bad Behavior. This combo cuts down the spam and the referral spam. Using the current auth method of using the image is dangerous for if it is mininterpreted a long post could be lost and that would be bad for the poster.

    Take a look at both, worth your time.

    Jonathan

  4. gravatar

    Raybdbomb Said,

    June 3, 2005 @ 12:05 am

    I use spam karma 2 already, I’ll look at bad behavior, thanks.

  5. gravatar

    Jonathan Said,

    June 27, 2005 @ 8:59 am

    So then you ended up with something like this?

    function add_count($p_number) {
    $result = mysql_query("select postnumber, cntaccess from mostAccessed where postnumber = '$p_number'");
    $test = 0;
    $tmp = explode("&", $_SESSION["p"]);
    if(!in_array($p_number, $tmp)){
    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    $row[1] += 1;
    @mysql_query(”update mostAccessed set cntaccess = ‘$row[1]‘ where postnumber = ‘$row[0]‘”);
    $test = 1;
    }
    if ($test == 0) {
    @mysql_query(”insert into mostAccessed(postnumber, cntaccess) values(’$p_number’, ‘1′)”);
    }
    $_SESSION["p"] .= $p_number; }
    }

  6. gravatar

    Raybdbomb Said,

    June 28, 2005 @ 1:52 pm

    Similar to that, yes. Here it is exactly:

    $tmp = explode("&", $_SESSION["p"]);
    if(!in_array($p_number, $tmp)){
            $result = mysql_query(”select postnumber, cntaccess from mostAccessed where postnumber = ‘$p_number’”);
            $test = 0;
            while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
                    $row[1] += 1;
                    @mysql_query(”update mostAccessed set cntaccess = ‘$row[1]‘ where postnumber = ‘$row[0]‘”);
                    ++$wpdb->num_queries;
                    $test = 1;
            }
            if ($test == 0) {
                    @mysql_query(”insert into mostAccessed(postnumber, cntaccess) values(’$p_number’, ‘1′)”);
                    ++$wpdb->num_queries;
            }
            $_SESSION["p"] .= “$p_number&”;
    }
    else{
            //already seen this post, don’t increment count
    }

    The only major difference I saw with yours is that you want $_SESSION["p"] to be “$p_number&” instead of just $p_number else the explode function won’t work correctly, as it’s looking for a “&” delimiter.

    Let me know if you have any problems.

  7. gravatar

    hpn Said,

    August 29, 2005 @ 4:01 pm

    well, the plugin no longer works fine for wordpress 1.5.2 since apparently the plugin functions are loaded later on… anybody worked on this?

  8. gravatar

    Raybdbomb Said,

    August 29, 2005 @ 4:53 pm

    I haven’t yet upgraded, I’ll try it and let you know what I had to do…

  9. gravatar

    AntySPAM Said,

    June 16, 2006 @ 5:00 am

    Esli Vy chitaete eto soobshenie znachit vasha gostevaya, forum ili blog popali v spamerskii spisok rassylok. Chtoby udalit’ svoi resurs iz etogo spiska prishlite ego url na email: antyspam@bk.ru Eto v vashih zhe interesah. Vse eshe mozhno popravit’. Nam nuzhny tol’ko zabroshennye resursy. Esli vash eshe razvivaetsya - my ostavim ego v pokoe.

  10. gravatar

    Raybdbomb Said,

    June 16, 2006 @ 11:22 am

    I don’t understand what you’re saying. Translate it and come back.

  11. gravatar

    Doug Noon Said,

    July 13, 2006 @ 2:28 am

    I really like this plugin, but there is a modification I want to make, and I’m afraid to mess with it because it’s working almost perfectly. I have it on my sidebar under the heading “Beaten Path” and for whatever reason a lot of people are accessing my “About” pages, which have their links already displayed above this part. I’m wondering how to exclude individual posts that I may not want counted. It seems as if I should be able to add a condition to the parameters in…

    foreach ($results as $result) {
    $postnumber = $result->postnumber;
    $post = @$wpdb->get_row(”SELECT ID, post_title FROM $tableposts WHERE ‘$postnumber’ = ID”);

    …so that specified posts are not printed. Am I correct that I could just add to the WHERE something like “and ‘$postnumber is not THIS, or THIS” ?

    My SQL knowledge is weak, and I’m not sure how the syntax would actually look. I’m curious to know if I’m on the right track with what I’m trying to accomplish.

  12. gravatar

    Raybdbomb Said,

    July 13, 2006 @ 6:16 am

    You are correct, but you’ll want it in your plugin file, so that’s not the right SQL statement.

  13. gravatar

    Raybdbomb Said,

    March 31, 2007 @ 12:12 am

    No more spam.

RSS feed for comments on this post

blogtimes