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.

Published by


Dave Slusher is a blogger, podcaster, computer programmer, author, science fiction fan and father. Member of the Podcast Hall of Fame class of 2022.

17 thoughts on “Popularity Contest Hacked”

  1. tiltededge says:

    I predict the next most popular thread will be on talking urinal cakes.

  2. Leo Maia says:

    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!

  3. dave says:

    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
    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).'">'
    else {
    $wpdb = $temp;
    Somewhere towards the bottom with the similar functions, add this:

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

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

  • buzzdroid says:

    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.

  • dave says:

    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.

  • ales says:

    ok, now how would I do this for 7 days instead of 30.

  • ales says:

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

  • Maski says:

    After a little code fighting i finally got it working… thanks

  • Scott says:

    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?

  • Ash Haque says:

    Exactly what I’ve been looking for! Big thanks

  • snowcrash says:


    Will your hack work for WP 2.3.3?
    As the original plugin has been upadated on Alex King’s page


  • Vince says:

    Just tried it on my website, http://offmanhattan.com

    Your zip file isn’t downloading so i had to go from what you had in the comments and it failed.

    Any way you could get the zip file up again please?


  • dave says:

    Vince, I fixed the downloading problem and you can get it again. Thanks for pointing out the problem.

  • Comments are closed.