Query another blog on your site

I wrote some simple code so that you could query posts from another blog on your site by using the argument blogid in your query

function blogid_query_set_blog_id( $query ) {
 global $wpdb;

 if ( isset($query->query_vars['blogid']) && $query->query_vars['blogid'] != $wpdb->blogid ){
 $query->original_blog_id = $wpdb->blogid;
 $wpdb->set_blog_id( $query->query_vars['blogid'] );
 }
}
add_action( 'pre_get_posts', 'blogid_query_set_blog_id' );
add_action( 'loop_start', 'blogid_query_set_blog_id' );

function blogid_query_restore_blog_id( $query ) {
 global $wpdb;

 if ( isset($query->query_vars['blogid']) && isset($query->original_blog_id) )
 $wpdb->set_blog_id( $query->original_blog_id );
}
add_action( 'loop_end', 'blogid_query_restore_blog_id' );

function blogid_query_posts_results( $posts, $query ) {
 blogid_query_restore_blog_id( $query );
 return $posts;
}
add_filter( 'posts_results', 'blogid_query_posts_results', 10, 2 );

So how does it work?

query_posts( 'blogid=3' );

It work pretty well with little, or no testing so far. Please write in the comments if you find any problems with it!

Advertisements
About

Web Developer @ Oakwood Creative - www.oakwood.se

Tagged with: , , ,
Posted in Intermediate
20 comments on “Query another blog on your site
  1. brad says:

    Love the script, thanks!

    One problem I run into is that the_permalink() is not outputting the proper link, causing the user to go to a broken (404) page.

    I am using it in the sidebar of my main site and I would like the read more link to direct the user to the appropriate post on the sub-blog.

    Any thoughts?

    Example:
    the_permalink() outputs the following link:
    ../blog/general/new-header-two/

    but should output this link as this is the link for the post:
    ../blog-iso/test-category-name/new-header-two/

    • Vincent says:

      Yes, using permalinks and other template tags such as the_tags and what not, will give you some trouble. This can be avoided however with filters, but it can become quite tedious in the long run. In your situation you could add filters to post_link and post_type_link if you are using custom post types.

      Maybe something like this could work:
      function mytheme_get_permalink( $permalink, $post ) {
      global $wpdb, $wp_query;

      if ( is_admin() )
      return $permalink;

      $queried_blog = $wp_query->query_vars['blogid'];
      $permalink = get_blog_permalink( $queried_blog, $post->ID );

      return $permalink;
      }
      add_filter( 'post_link', 'mytheme_get_permalink', 10, 2 );

      Haven’t tested it, but it might give you an idea!

      • brad says:

        Thanks for the quick reply. The additional script returned nothing, but I tried this variation and it worked. If you see anything wrong with using this directly in my script, pls lmk, but it seems to be working well.

        replaced the_permalink(); with ID);?> where 7 is the blog id in my query.

        Cheers!

      • brad says:

        The answer I posted seems to have been cutoff, whoops. Here it is again:
        Changed the_permalink();
        to
        echo get_blog_permalink(7,$post->ID);

      • Vincent says:

        Great. Hope I helped!

      • brad says:

        Hi, Vincent.

        I just realized the date being retrieved is not the correct date for the blog post. Do you know of a function or some other way to retrieve the blog post date for the post that is being retrieved by your function?

      • brad says:

        I can get the date using – $post->post_date; – but I can’t format it using date();

        Any thoughts? I tried something like date(“F d, Y”, $post->post_date); and it didn’t work..

      • Vincent says:

        ‘date’ is a PHP-function where the second argument needs to be a time stamp. You would be better of using the_date(‘F d Y’)

    • brad says:

      perfect, thanks!

  2. vexill says:

    is there anyway to only display from a certain category?

  3. Well, this is pretty amazing, fantastic! I have made this work even with custom posts/taxonomies! It is pretty simple to query many different things, example:

    However, I got stuck in something waaaay more advanced for my level of understanding… How to make the permalink work? Let me explain:

    I am in the blog #1, for example, a Zoo blog.
    I want to query my blog 2 for my animal list, felines.
    My blog #2 is just a sort of a catalog. I don’t want to navigate and browse it directly.

    Great, all set…
    Then, in a a page in the blog 1 I print the contents:

    …and… (using a solution already presented here)
    ID); ?>

    It works GREAT, however, the permalink above throws me to inside the blog #2 permalink page, and as I said before, it is just my catalog. I don’t want to navigate on it.

    So, here is the question…
    How to make the original work?
    Its target is what I want – it keep focus on blog #1 – however, since there is no such post in blog #1, it returns me a 404 error.

    Maybe there is a way to force the blog #1 single.php to display the content from the custom blog #2 query…

    Any ideas, people?
    Thank you very much for your attention.

    Cospefogo.

  4. Ouch… My code was not displayed. Let me try again…
    Dear moderator, please remove my previous comment!
    Thanks!

    * * *

    Well, this is pretty amazing, fantastic! I have made this work even with custom posts/taxonomies! It is pretty simple to query many different things, example:

    query_posts( ‘blogid=2&tag=news’ );
    query_posts( ‘blogid=6&category_name=comics&tag=hulk’ );
    query_posts( ‘blogid=2&post_type=recipes&category_name=pizzas&tag=margerita’ );

    However, I got stuck in something waaaay more advanced for my level of understanding… How to make the permalink work? Let me explain:

    I am in the blog #1, for example, a Zoo blog.
    I want to query my blog 2 for my animal list, felines.
    My blog #2 is just a sort of a catalog. I don’t want to navigate and browse it directly.

    query_posts( ‘blogid=2&category_name=animals&tag=feline’ );

    Great, all set…
    Then, in a a page in the blog 1 I print the contents:

    the_title();
    the_author();
    echo get_the_date(‘Y-m-d’);
    …and… (using a solution already presented here)
    echo get_blog_permalink(2,$post->ID);

    It works GREAT, however, the permalink above throws me to inside the blog #2 permalink page, and as I said before, it is just my catalog. I don’t want to navigate on it.

    So, here is the question…
    How to make the original the_permalink(); work?
    Its target is what I want – it keep focus on blog #1 – however, since there is no such post in blog #1, it returns me a 404 error.

    Maybe there is a way to force the blog #1 single.php to display the content from the custom blog #2 query…

    Any ideas, people?
    Thank you very much for your attention.

    Cospefogo.

  5. Good morning. I have a question. where I have to insert this code?
    Thanks a lot

  6. I need all the blogs updates to be displayed in the activity of the community.Thanks a lot

  7. I would like to display updates of blogs in the area of activity of the community. For example in Home Page:
    [template_part slug=”template-bp” name=”activity-loop” query_posts( ‘blogid=3,8’ )]

  8. MikeNGarrett says:

    I want to have your babies.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

About

WordPress Quick Tips is a blog supplying great tips about WordPress.

We hope to create a great knowledge resource for WordPress developers as well as serving a reminder for all the forgetful ones.

The blog is created and run by Vincent of Oakwood Creative

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 120 other followers

%d bloggers like this: