The MU forums have moved to WordPress.org

How to run a loop on a different blog (4 posts)

  1. DaveMee
    Member
    Posted 14 years ago #

    Hello everyone!

    I have a setup where the primary (blog 1 in the databases) is a route into the other blogs.

    Certain posts are marked as being aggregators and associated with another blog ID; I've built a plugin which handles this already, associating a blog ID with a post on user request.

    For posts, I want to be able to run an extra loop, which pulls the latest (X) posts from the associated blog, and allows me to use the standard templating features to display their content.

    I could do this with a raw SQL query, but then I'm missing the hooks to format the content. What I really want to do is run something like

    query_posts('blog_id=3&showposts=3');

    Does anyone know an clean way of doing this, or am I SQL dirty? (Or I could pull the RSS in from the other site, I know, but again it's a weird alternative to what seems like it should just be another parameter on query_posts).

    Thanks for any insight, wizards!

  2. DaveMee
    Member
    Posted 14 years ago #

    Okay, so I just found out about switch_to_blog, but it's not restoring to the post I'm using when I restore_current_blog().

    I'm doing this...

    if($associatedsite) {
    		switch_to_blog($associatedsite);
    		query_posts('showposts=10');
    		while (have_posts()) : the_post(); ?>
    				<span id="post-<?php the_id(); ?>" <?php post_class(); ?>><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></span>
    <?php
    		endwhile;
    		restore_current_blog();
     	}

    ... okay, a little homework, solved, if it's of any help to anyone in a similar position.

    Before using the above code, make sure you assign $post to another variable, then assign it back after running the above code.

    Is there a better way to handle this, Bueller?

  3. DeannaS
    Member
    Posted 14 years ago #

    Nope, I think that's probably the way to handle it - based on what you want.

  4. DaveMee
    Member
    Posted 14 years ago #

    Thanks for your feedback, DeannaS.

About this Topic