Hefta-Gaub Development Blog

March 11, 2007

WPMU- Top Posts Plugin

Filed under: development, Wordpress plugins, WordpressMU — zappoman @ 7:46 am

I’ve hacked together a prototype of a Top Posts plugin for WordPressMU.

The concept is to create a global table that stores the blog_id, post_id, and time for each hit to a post/page, and then allows you to get list of the posts with the most hits. This is an early early version.

You can download it here.


Put this php file in your ‘wp-content/mu-plugins’ directory. Sit back and enjoy.


The simple version would be to call:

<?php zap_top_posts_html(); ?>

This will return a <ul>…</ul> block of html.

But you can also use this much like you would use get_posts() in any template. For example, the following should also work…

 $myposts = zap_get_top_posts();
 foreach($myposts as $post) :
    <li><a href="<?php echo $post->guid; ?>">
	<?php the_title(); ?></a>
	[<?php zap_the_post_hits(); ?>]</li>
 <?php endforeach; ?>

Anyway, this is just a prototype. I haven’t really done that much testing. I have no idea how well it will perform in general. I know there are a couple bugs in there. As I fine tune this, I will post additional updates.

Known issue: You can’t use the get_permalink() and the_permalink() tags becuase they depend on other global variables that are not properly set up with these functions. If you want the link to the post use $post->guid.



  1. Great idea, though it needs some more developing…
    I tried it, but it didn’t seem to fetch posts from any other blog than the main blog.

    Comment by henry — March 27, 2007 @ 1:02 pm

  2. Henry, Thanks for the heads up, yes I noticed that the the_permalink() template doesn’t always work. I have this code working by using the code below… and it shows the posts from multiple blogs just fine.. I have noticed that sometimes some of the tag templates don’t always work, so if you could tell me more about the code you used it would help me debug it…

    Did you set $post to global?

    here’s an example of what I currently have running…


    global $post; /* needs to be global if you want get_permalink() and get_the_title() to work. */

    $top_posts = zap_get_top_posts();


    foreach($top_posts as $post)
    $html.="guid."' title='".$post->guid."'>".get_the_title()."[".$post->post_hits."]";
    echo $html;

    Comment by zappoman — March 27, 2007 @ 3:03 pm

  3. I’ve looked into the issue that Henry mentioned, and it appears as if the the_permalink() and get_permalink() are at least two functions that depend on another deeper global variable inside of wordpressMU called $current_blog. So my original understanding that simply setting the global $post would work does not.

    However, have no fear… if you stick with using $post->guid, you should get the correct link. I’ve changed the example above to reflect this.

    Comment by zappoman — March 27, 2007 @ 5:12 pm

  4. The setup-function collides with creation of new blogs, because the require_once of “upgrade-functions.php” collides with the same line in wpmu_functions.php/install_blog. I commented out the body of that function after the table had been created, but someone a bit more WP-savvy should devise a permanent solution

    Comment by Nis Jorgensen — March 28, 2007 @ 10:26 am

  5. Nis, thanks for the heads up. I will look into this…

    Like I said, I hadn’t tested it extensively, I guess I haven’t created a new blog since adding this. Kinda silly to have not tested that? Eh? 😉

    Comment by zappoman — March 28, 2007 @ 4:46 pm

  6. Changing the setup function as follows will address this problem. I will update a patch to this later… Thanks Nis for pointing this out.

    * Method: setup()
    * Purpose: creates our tracking table if needed.
    function setup()
    * make sure the upgrade-functions for maybe_create_table is available
    if (function_exists('maybe_create_table'))
    $table_hits_query = "CREATE TABLE $this->table_hits (
    id int(11) unsigned NOT NULL auto_increment,
    blog_id BIGINT(20) unsigned NOT NULL,
    post_id BIGINT(20) unsigned,
    hit_time int(10) unsigned NOT NULL default '0',
    UNIQUE KEY id (id)
    maybe_create_table($this->table_hits, $table_hits_query);

    Comment by zappoman — March 28, 2007 @ 4:55 pm

  7. Note, I’ve updated the code to 0.42.1 with the following changes…

    Small bug fix to ‘maybe_create_table’ behavior. Namely, we used to try to load it if this function wasn’t available, and now we simply check that the function is available.

    Added blog_id to the posts returned by get_top_posts. This can be useful in forming correct permalinks to your blog posts.

    Added function zap_setup_post_globals() which in some cases will setup the globals for other wordpressmu templates to work, but this doesn’t always reliably work.

    Comment by zappoman — April 6, 2007 @ 12:49 am

  8. I have commented out the call to “update_post_caches”, since this caches all the posts in the cache for the current blog. I am not sure exactly why we would need to call this, but the comment above it seems to indicate that the author did not consider it too important either.

    Comment by Nis Jorgensen — April 18, 2007 @ 1:53 pm

  9. Hey Zappoman—

    Nice script! I’ve learned some cool things about writing an object-oriented plugin for WordPress.

    I found one bug — the hits for deleted blogs stay in the `top_hits` table, which causes the FK constrain to break. Here’s the patch I wrote for that (tested on my beta site; seems to work 😉 ):

    — wpmu-topposts.php 2007-06-04 16:36:07.000000000 -0700
    +++ wpmu-topposts-fredcode.php 2007-06-04 16:35:10.000000000 -0700
    @@ -685,6 +685,17 @@

    return $html;
    + /*
    + * Method: delete_blog
    + * Purpose: Removes a blog’s entries from the top_hits table.
    + */
    + function delete_blog($blog_id, $drop) {
    + global $wpdb;
    + if ($drop)
    + $wpdb->get_results(“DELETE from $this->table_hits where blog_id=’$blog_id'”);
    + }

    // This will be our main “tracking object” we will keep things nice and
    @@ -698,6 +709,8 @@
    // This hook will be called for every page.
    add_action(‘shutdown’, array(&$zap_wpmutp, ‘recordhit’));

    +// This hook will be called when a blog is deleted
    +add_action(‘delete_blog’, array(&$zap_wpmutp, ‘delete_blog’));

    * Function: zap_get_top_posts()

    Comment by Fredcode — June 5, 2007 @ 5:42 am

  10. Thanks FredCode… that’s a good mod. I actually have made some related changes to the code but it’s not quite ready to release yet.

    The change I made was to “keep trying” to get posts if the posts that come back from the table_hits query are missing. This can happen in the case of posts being deleted (but not the whole blog being deleted) as well.

    My approach was to just keep doing recursive queries until I got enough posts to fulfill the requested $numberposts… But maybe using hooks is a better approach.

    I’ll do a release soon with a bunch of new features including caching.

    Comment by zappoman — June 5, 2007 @ 6:37 am

  11. Yeah, it makes sense to me to make incremental updates to the top_hits table as posts are deleted — a slight overhead at the “front-end” of deleting the post in exchange for less work to fetch the posts.

    However, even doing that, you’ve still got to cover the case where the post / blog has been deleted and your hooks (for whatever reason) didn’t run. I’d suggest throwing in a JOIN to the wp_*_posts tables on the SELECT, but that could be a tedious chore to write and debug.

    Look forward to your next release. Keep on hackin’, dude!

    Comment by Fredcode — June 22, 2007 @ 12:21 am

  12. Hey, i Love this plugin, just a little suggestion, it would be great if the hits had delay, i mean that the if the hit was not counted in the loading moment but 30 second after loading and staying on the page it would bring up more accurate results of popularity and interestingness.

    Just i suggestion, sorry my english i dont speak it very well 🙂 i love youre plugins 🙂


    Comment by Ana — June 26, 2007 @ 10:45 pm

  13. Installed the Plugin, inserted the PHP tag, and got this:
    [Table ‘wordpress.1_posts’ doesn’t exist]
    SELECT *,’2′ AS ‘post_hits’,’1′ AS ‘blog_id’ FROM 1_posts WHERE ID = 13


    Comment by Tomer — July 28, 2007 @ 1:09 pm

  14. Tomer, are you using WordPressMU? This looks like something is broken in the core classes… or as if you are using WordPress instead of WordPressMU.

    Comment by zappoman — July 28, 2007 @ 11:25 pm

  15. Great plugin! Just what I have been searching for. I’ve noticed 1 bug though that is easily fixed. At the end of get_top_blogs_html() you have $html.=$before_list; instead of $html.=$after_list; which results in instead of creating invalid html. Thanks again!

    Comment by FatBits — October 5, 2007 @ 1:28 am

  16. Thanks for sharing FatBits… yes, that’s a bug alright…

    Actually, I’ve significantly changed this plugin since I released it… but I haven’t released a new version in a while. I should really get around to that some day. 😉

    Comment by zappoman — October 5, 2007 @ 3:07 am

  17. […] Visit […]

    Pingback by WordPress Plugins Database » Plugin Details » zappo_wpmu_topposts — October 17, 2007 @ 12:36 pm

  18. Hi

    I am using wpmu 1.3, i am using zappo_wpmu_topposts and using the following, the plugin is in the mu-plugin dir;

    I get no results at all,

    Comment by gordo — November 15, 2007 @ 10:33 am

  19. This plugin sounds like what I’m looking for, except that I would love to be able to call get_top_posts() and limit the result to just one blog whose ID I would pass as a parameter.

    Comment by Jacob Share — November 20, 2007 @ 8:58 pm

  20. Great Plugin, tested with wpmu 1.3 fine, couple things

    1. How can i exclude Hello world! posts from showing in results from zap_get_top_posts?
    2. Do you have example of multiple ways i can format the results to show blogname, post name and various ways of showing the results?

    Thanks for the great work

    Comment by gordob — November 22, 2007 @ 7:45 am

  21. Nice script, but it has some problems:

    It doesn’t quite work right with version 2.3.3 of WPMU. I get two blog hits for each view, and sometimes the post_id gets stuck at the same number. I had to use this code to make it work properly:

    $urlRequested = “http://” . $_SERVER[‘SERVER_NAME’] . $_SERVER[‘REQUEST_URI’];
    $post_id = url_to_postid($urlRequested);

    There’s an easy way to combat potential fraud as well. Just change the timestamp to only use month-day-year, add an ip_addr column, remove the unique key id column, make blog_id+post_id+hit_time+ip_addr the new unique, then finally change your insert to a replace.

    That change will only allow each ip address to count one hit per post per day, and one hit per blog per day.

    Comment by NK5 — March 11, 2008 @ 6:15 am

  22. I found a better, more reliable way to get the correct post id in version 2.3.3:

    global $blog_id,$posts, $single;
    if($single == 1) {

    Comment by NK5 — March 12, 2008 @ 5:05 am

  23. Does your blog have a contact page? I’m having trouble locating it but, I’d like to shoot you an email. I’ve got some recommendations for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it expand over time.

    Comment by electronics online — September 1, 2011 @ 11:17 am

  24. Thanks for the auspicious writeup. It in reality was a amusement account it. Glance advanced to more delivered agreeable from you! By the way, how could we keep up a correspondence?

    Comment by projeto sobre o dia dos pais — September 9, 2011 @ 5:18 am

  25. Pretty! This has been an extremely wonderful post.
    Many thanks for providing this info.

    Comment by More Help — August 7, 2012 @ 2:40 am

  26. Thanks on your marvelous posting! I truly enjoyed reading it, you are a
    great author. I will remember to bookmark your blog and definitely will
    come back down the road. I want to encourage you to definitely continue your great
    writing, have a nice day!

    Comment by edwingallery.com — June 22, 2013 @ 8:03 pm

  27. I do not know whether it’s just me or if everybody else experiencing problems with your website. It looks like some of the text within your posts are running off the screen. Can someone else please provide feedback and let me know if this is happening to them as well? This might be a issue with my browser because I’ve had this happen before.
    Appreciate it

    Comment by buy youtube views — July 22, 2013 @ 4:26 pm

  28. The toothbrush should comfortably fit in the mouth to form plaque.
    If you have an open wound in your mouth by two simple weapons:
    toothbrush and toothpaste. Moreover, a thorough professional cleaning
    of the teeth and inspect kids beds the mouth for x-raying.
    In that case, you must not worry because there are no underlying problems
    that could last a lifetime, kids beds in reality, restorative appliances such as dentures.
    After some work on your part, you may be following the directly precisely, you could still be looking at another thousand or more.

    Comment by Jayme — August 1, 2013 @ 11:31 am

  29. Usually I do not learn article on blogs, however I wish to say that this write-up very pressured me to
    check out and do so! Your writing style has been surprised
    me. Thanks, very nice article.

    Comment by best trampolines — August 6, 2013 @ 8:43 am

  30. What a perfect website. Thank you for spending the amount of time.

    Comment by trampoline workout — August 6, 2013 @ 10:39 am

  31. Unquestionably believe that that you said. Your favorite
    reason seemed to be on the internet the easiest thing to remember of.
    I say to you, I definitely get irked at the same time as folks think about worries
    that they just do not recognise about. You managed to hit the nail upon the
    highest as smartly as defined out the entire thing without
    having side effect , other folks could take a signal.
    Will probably be again to get more. Thanks

    Comment by Keisha — August 7, 2013 @ 5:06 pm

  32. Thanks for the marvelous posting! I truly enjoyed reading it, you could
    be a great author. I will be sure to bookmark your blog and definitely will
    come back from now on. I want to encourage you continue your great work,
    have a nice holiday weekend!

    Comment by best vitamins to build muscle — August 20, 2013 @ 2:52 pm

  33. Great blog! Is your theme custom made or did you download it from somewhere?

    A theme like yours with a few simple tweeks would really make my blog shine.
    Please let me know where you got your theme. Thanks

    Comment by diesel engines how they work — March 2, 2014 @ 12:41 am

  34. Hi there, just became alert to your blog through Google,
    and found that it’s really informative. I’m going to watch out for brussels.

    I will appreciate if you continue this in future. Many people
    will be benefited from your writing. Cheers!

    Comment by Landon — June 10, 2014 @ 4:53 am

  35. This blog was… how do you say it? Relevant!! Finally I’ve
    found something which helped me. Thanks a lot!

    Comment by condo for sale — June 5, 2016 @ 5:49 pm

  36. This piece of writing provides clear idea in favor of the
    new visitors of blogging, that genuinely how to do blogging.

    Comment by accurate horoscopes — July 20, 2016 @ 3:17 am

  37. I read this paragraph completely on the topic of the comparison of newest and
    preceding technologies, it’s awesome article.

    Comment by Miyanari D. — September 3, 2016 @ 1:04 am

  38. Hi friends, how is everything, and what you would like to say
    concerning this post, in my view its in fact remarkable in favor of me.

    Comment by colonial house — October 3, 2016 @ 9:50 am

  39. Havee you ever thought about publishing an ebook or guest authoring onn other sites?
    I have a blog based on the same subjects you discuss and would really like tto have you share some stories/information. I know my readers would appreciate your work.
    If yyou are even remotely interested, feel free to
    send me aan email.

    Comment by Virafend.Org — October 3, 2016 @ 8:05 pm

  40. Many times a couple of experiences dissatisfaction and
    decides to end their bond as opposed to probing to determine the basis cause.
    All of these might be considered points where seeking help is sensible and frequently,
    it is simply from the eyes of your outside that each party in the troubled relationship
    are able to see the issues. The theory goes
    how the more femininity a couple of has, the deeper their bond becomes.

    Comment by escort birmingham — October 8, 2016 @ 7:47 pm

  41. Hello there I am so happy I found your web site, I really
    found you by error, while I was looking on Askjeeve for something else,
    Anyways I am here now and would just like to say thank you for a incredible post and a all
    round thrilling blog (I also love the theme/design), I don’t have time to browse it all at the minute but I have bookmarked it and also added your RSS feeds, so when I have time I will be back to read much more, Please do keep up
    the great b.

    Comment by grosir gamis batik muslim — October 16, 2016 @ 8:55 am

  42. I blog often and I gehuinely thank you for youyr information. This great article has
    really peaked my interest. I will bookark your blog and keep checking for new information about once a week.
    I opted in for your RSS feed as well.

    Comment by ufc — October 17, 2016 @ 3:40 pm

  43. I loved as much as you’ll reeceive carried ouut right
    here. The sketch is attractive, you authored material stylish.
    nonetheless, you command get ggot an edginess over
    that yyou wish bee dedlivering the following. unwell unquestionably come
    more formerly again ass eactly the same nearly very often inside
    case you shield this hike.

    Comment by top lightening creams — October 24, 2016 @ 1:53 am

  44. Do you mind if I quote a couple of your articles as long as
    I provide credit and sources back to your webpage? My website is in the exact same niche as yours and my visitors would definitely benefit from some of the information you provide here.
    Please let me know if this alright with you. Regards!

    Comment by clash royale cheat — December 22, 2016 @ 4:02 pm

  45. What’s up to every one, the contents existing at this website are actually remarkable for people knowledge,
    well, keep up the nice work fellows.

    Comment by 0546 sohbet — January 9, 2017 @ 9:27 pm

  46. What i do not understood is in fact how you are no longer
    really much more well-preferred than you may be right now.
    You are so intelligent. You know therefore significantly on the subject of this topic, made me
    individually imagine it from numerous various angles.
    Its like women and men don’t seem to be involved except it’s something to do with
    Girl gaga! Your individual stuffs great. All the time care for it up!

    Comment by 0546 sohbetleri — January 9, 2017 @ 10:04 pm

  47. If you would like to take much from this post then you have to apply these techniques to your won webpage.

    Comment by bizi sikin pornosu izle — January 10, 2017 @ 12:46 pm

  48. Hi there to every one, for the reason that I am really eager of reading this blog’s post to be updated on a regular basis.
    It includes nice data.

    Comment by sex — April 18, 2017 @ 8:13 am

  49. Hey There. I found your weblog the usage of msn. This is an extremely neatly written article.
    I will be sure to bookmark it and come back to learn extra of your useful information. Thank you for the post.
    I will certainly return.

    Comment by exclusive cards — April 27, 2017 @ 5:07 pm

  50. Hello to every one, the contents present at this website are really amazing for people experience, well,
    keep up the nice work fellows.

    Comment by mandmdirect.com — May 17, 2017 @ 1:47 am

  51. I believe everything wrote was very reasonable. But, think on this, suppose you
    typed a catchier title? I mean, I don’t wish
    to tell you how to run your blog, however what if you added a headliune that grabbed
    people’s attention? I mean WPMU- Top Posts Plugin |
    Hefta-Gauub Development Blog iis kinda vanilla. You ought to glance at Yahoo’s home page and see how they create article headlines to
    grab people to open the links. You might add a video
    or a related pic or two to grab readers excited about what you’ve got too
    say. In my opinion, it would make your posts a little livelier.

    Comment by αστρολογια λεφακης — July 5, 2017 @ 4:04 am

  52. This site really has all the information I needed concerning this subject and
    didn’t know who to ask.

    Comment by proxy scraping urls — August 30, 2017 @ 8:41 pm

  53. Hi there everyone, it’s my first pay a quick visit at
    this web page, and post is in fact fruitful for me, keep uup posting these posts.

    Comment by παπουτσια θεσσαλονικη — October 23, 2017 @ 7:40 pm

  54. situs Slot online terbaik

    WPMU- Top Posts Plugin | Hefta-Gaub Development Blog

    Trackback by situs Slot online terbaik — July 1, 2022 @ 2:00 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: