New Plugin: MU-Helpers

This plugin may already be obsolete by the time you read this, but I found a couple of functions that weren’t provided by the default WordPress-MU API that you may find useful in your own WordPress-MU development.

Download MU-Helpers Plugin

The first function, get_all_blog_posts, will return all the posts across all of the blogs on your site. It employs a quicksort algorithm to ensure that the list is in the order you specify, and works pretty darn fast, all things considered. The function signature looks like this:

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

You can specify the number of posts per blog, how they should be ordered in the original query, and how they should be sorted by the quicksort algorithm. The orderby param takes the same values as query_posts would. The sort param is the column name in the database you want to sort on.

The second function, get_all_blog_comments, queries for all of the comments on all of the blogs on your network. It employs the same quicksort algorithm to sort the returned list by whichever column you specify. The function signature looks like this:

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

You can specify the number of comments per blog, how they are ordered per blog and how the whole list is sorted. The orderby param takes the same values as get_comments. The sort param is the column name in the database you want to sort on.

This plugin has not been tested with the new WordPress 3.0 Multi-Site Network.

16 thoughts on “New Plugin: MU-Helpers

  1. I’ve got a “digest” page that lists authors and their bio, and links to their blog.

    I want to be able to specify their blog ID and fetch a list of posts to display recent posts from each author. Each author has their own blog.

    The digest page is located on the main site (ie: site.com), while each authors blog is site.com/authorname/

      • Yah, I’ve got the blog ID’s ahead of time. I get them via get_userdata() after I have a list of author ID’s. I was afraid switch_to_blog was the only way. I’m not going down that road, may just be easier for me to pull in the RSS feeds.

        Thanks Eric!

        • switch_to_blog is the only way, as far as I know, to query posts from a blog you’re not currently on. The functions in MU-Helpers makes extensive use of them. Parsing the RSS feeds may seem easier, but it sure is going to be a lot more expensive in terms of efficiency. Good luck!

          • Pulling RSS feeds will be less efficient than using switch_to_blog? Everything I’ve read seems to imply using switch_to_blog in a loop will devour resources.

            I suppose I’ll just use switch_to_blog if you think that’d be the best route.

            Thanks again Eric!

          • Depends on the loop. Both MU-Helper functions loop over a list of blog IDs, and use switch_to_blog/get_posts to query for each blog’s posts. I used a quicksort algorithm to mash them together in an ordered list. You can take the same approach here. Just limit your queries, and pull back only what you need to keep the process manageable. If you are caching queries (at the mysql level) and caching your results, you can optimize it even further.

    • The way it works is that each blog is queried for the number of posts required, and then merged into one array of posts. The quicksort algorithm is then applied to the array so that the list of posts are sorted correctly. There is one efficient query per blog, but it only pulls in what you need (i.e. limited to just N posts) and shouldn’t be very expensive (even for a large number of blogs). The quicksort was used to avoid complex UNIONS and other MySQL nonsense that would have made getting this data use a lot more resources than it was worth.

        • You’d basically replace the entire loop with this. I wrote these for a couple of widgets, so I was looping over the results directly.


          $posts = get_all_blog_posts();
          foeach ($posts as $post):
          echo $post->post_title;
          endforeach;

          The template tags may or may not work inside the foreach loop shown here. I haven’t tested.

  2. This is a great plugin! I just hacked get_all_blog_posts() a bit for a friend so she can grab the blog ID, title, and URL right from the post objects. (I’m sure you could get all that later using just the ID, but for her purposes, this was much easier.)


    function get_all_blog_posts( $num_per_blog = 1, $orderby = 'date', $sort = 'post_date_gmt' )
    {
    $posts = array();
    $blogs = get_blog_list( 0,'all' );
    foreach ( $blogs as $blog ):
    switch_to_blog($blog['blog_id']);
    $newposts = get_posts('orderby='.$orderby.'&numberposts='.$num_per_blog);
    foreach ($newposts as $postobj) {
    $postobj->blog_id = $blog['blog_id'];
    $postobj->blog_name = get_bloginfo('name');
    $postobj->blog_url = get_bloginfo('url');
    }
    $posts = array_merge( $posts, $newposts );
    restore_current_blog();
    endforeach;

    return _quicksort( $posts, $sort );
    }

  3. Pingback: WordPress Plugin Releases for 06/18 | Tolly Blog

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">