Developer’s Corner: Useful MU Helper Functions for WPMU and BuddyPress

This week a new plugin for WPMU and BuddyPress quietly slipped into the repository with hardly a mention. The MU-Helpers plugin was written by Eric Marden to help make your life easier when developing solutions for WPMU and BuddyPress. Currently the plugin provides two functions that WPMU developers cannot live without: a function to return all posts from all blogs and one to return all comments on all blogs.

Get All Blog Posts

This function returns a sorted array of all posts across all blogs on the MU install using a quicksort algorithm to ensure that the list is in the order you specify. It uses the same Order By Paramaters as query_posts. The sort param is the column name in the database you want to sort on.

Usage:

1
get_all_blog_posts( $num_per_blog = 1, $orderby = 'date', $sort = 'post_date_gmt' )

Get All Blog Comments

This function returns a sorted array of all comments across all blogs on the MU install. It uses the same Order By Paramaters as get_comments.

Usage:

1
get_all_blog_comments( $num_per_blog = 1, $orderby = 'comment_date_gmt', $sort = 'comment_date_gmt' )

Please note that this plugin has not been tested with the new WordPress 3.0 Multi-Site Network. If you have a 3.0 MU install, feel free to test and let us know in the comments how it’s working.

MU-Helpers was created to be an evolving library of functions that will help to make life easier for developers. It’s still under active development. You can take a peek at its code and get involved in contributing to the project at github.com, where it’s currently being hosted. This is one plugin you’re going to want to follow as contributors add more useful functions that will save you time.

Comments (3)

  1. With a quick glance of the code, I would HIGHLY recommend against using those functions. Each will perform as many queries as you have blogs on your install for every page load.

    On a normal WPMU site with say 1000 blogs thats more than 1000 uncached queries on every page load. That would kill your site for a good while.

  2. Aaron thanks for the input. I was testing it out last night with very small WPMU sties (under 10 blogs). Maybe I should note this is not an option for large scale sites. Post/Commenet Indexer are a much safer way to go about this.

  3. Thanks for you input Aaron, but you are actually quite mistaken. The alternative to my approach would be to perform one big query to join all of the tables together – something that not only would be gnarly to write, and also quite inefficient to run. The series of simple selects performed here – with the sorting done later by the super-fast C-functions in PHP – is the ideal solution since these sql statements play into MySQL’s built in query cache (something that is almost always on by default, even on shared hosts) and is further enhanced by a more comprehensive WordPress caching strategy. I tested this out on a site with almost 600 blogs and it continued to run as fast when I had 6. More queries do not necessarily equal more overhead.

Participate