Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using WordPress 3.2.1 multi-user. I need to show posts from a specific sub domain in my home page, for example: http://mysite.com/sub-blog. I need to show posts from sub-blog in my home page.

Is their any special code to query from a specific blog?

share|improve this question
add comment

2 Answers

There are also two plugins you may find useful for this:

ThreeWPBroadcast and WP MU Sitewide tags

share|improve this answer
add comment

You're probably going to have to write some custom SQL for that. Here's an example function that would do this. It's slightly insecure as we can't use $wpdb->prepare to put the blog_id in (it surrounds anything inserted with quotes).

<?php
function wpse33779_get_posts_from_blog( $blog_id = 1 )
{
    // is this is the current blog, just get posts...
    if( 1 === $blog_id )
    {
        return get_posts();
    }
    else 
    {
        // make sure this is a number.
        $blog_id = absint( $blog_id );
    }

    // did absint kill our blog id?
    if( ! $blog_id ) return array();

    global $wpdb;
    $a = $wpdb->prepare( "SELECT * from {$wpdb->base_prefix}{$blog_id}_posts WHERE 'post' = post_type AND 'publish' = post_status" );
    $posts = $wpdb->get_results( $a );
    return $posts;
}

I'm not good at writing SQL, but you get the idea. You could flesh that function out to be as full featured as get_posts as well.

You could also use switch_to_blog

<?php
switch_to_blog( $blod_id );
$posts = get_posts();
// do stuff with posts
restore_current_blog();

That said, switch_to_blog is probably not a great idea. It's really slow.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.