Popularity Contest Hacked

I made myself a little hack of the Popularity Contest WordPress plugin. I was really sick of seeing that stupid “Rosie V. Trump” post at the top of the list for the last 3 months, so I added in a function that shows the most popular posts in the last 30 days. I suppose even better would be to make that parameterized so that you could call it with any number and have it look back that many days. It’s worth noting that this sort of thing already exists in the management pages, but I had to hack it to expose it to the public side. Maybe I’ll do that and then submit it back to Alex King to include it in the main release. Although, it took me 3 minutes to do it while unfamiliar with the code so I’m sure he could do it in 45 seconds.

Update: I tried to post the thing in coments and it styled it and it sucks. Here is a zip file of my hacked version. Replace your popularity-contest.php with the one from the zip file and then you’ll have a new function you can use in your template called akpc_most_popular_in_last_month() . Have at it, friends.

17 Replies to “Popularity Contest Hacked”

  1. Please tell me: how did you do it?

    I’ve been trying to the exact same thing for me. I can’t stand seeing my top post for the last 3 months every single day.

    Thank you in advance!

  2. Add these to your popularity-contest.php

    Right before the end of the class, put this:

    function show_top_ranked_in_last_month($limit, $before, $after) {
    global $wpdb;
    $temp = $wpdb;

    $join = apply_filters('posts_join', '');
    $where = apply_filters('posts_where', '');
    $groupby = apply_filters('posts_groupby', '');
    if (!empty($groupby)) { $groupby = ' GROUP BY '.$groupby; }

    $posts = $wpdb->get_results("
    SELECT ID, post_title
    FROM $wpdb->posts
    LEFT JOIN $wpdb->ak_popularity pop
    ON $wpdb->posts.ID = pop.post_id
    $join
    WHERE post_date > (SUBDATE(CURDATE(), INTERVAL '30' DAY))
    AND post_status = 'publish'
    AND post_date < NOW() $where $groupby ORDER BY pop.total DESC LIMIT ".intval($limit) ); if ($posts) { foreach ($posts as $post) { print( $before.'ID).'">'
    .$post->post_title.''.$after
    );
    }
    }
    else {
    print($before.'(none)'.$after);
    }
    $wpdb = $temp;
    }
    Somewhere towards the bottom with the similar functions, add this:


    function akpc_most_popular_in_last_month($limit = 10, $before = '

  3. ', $after = '
  4. ') {
    global $akpc;
    $akpc->show_top_ranked_in_last_month($limit, $before, $after);
    }

  5. Interestingly, we implemented your hack thinking this would show the post popularity according to the last 30 days (or 7 days, 10 days or whatever you choose to use as the interval). That is not what the hack does. This hack acts as a filter of sorts on what posts are shown in the list – the hack shows the popularity of the posts with a post date within the time period chosen.

    This might be confusing to some people who try to implement the hack. The hack works great to filter the post in the list and show only posts from the time period, but it does not show all posts according to their popularity figures accumulated during the time period.

    If you can do that, that would be kick ass.

  6. buzzdroid,
    You can’t do that with the plugin as it exists. It doesn’t keep individual hit information, just totals so the information you want to use isn’t there.

  7. duh.. ok, probably change query from (SUBDATE(CURDATE(), INTERVAL ’30’ DAY)) to (SUBDATE(CURDATE(), INTERVAL ‘7’ DAY))

  8. Big dummy that I am, I can’t figure out what the proper code for this hack actually is. Is there a version with no styling available somewhere?

Comments are closed.