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 working on a multisite blog network where I have to display recent post on my home page, 10 post per page navigation.

I did implement it using wpdb object with one complex sql statement.

  • I got blog ids us get_blog_list function
  • looped through each blog id list and generated a one single sql query to get post from all sites
  • Used wpdb->get_result to get list of posts from all subsites

I have recently started working on PHP and WordPress and am still learning it.

Is use of wpdb object is the right way in this situation? I have read in many articles which uses WP_Query object. I don't know how to use WP_Query object in this situation?

Help me implement this in right/better way.

share|improve this question
    
Please post you code. What is the significance of the 'multisite' part of the title? Are you pulling posts from one site into another? Or pulling posts from the same site? –  s_ha_dum Nov 29 '12 at 6:21

1 Answer 1

You could use your list of blog ids in this way ...

$posts = array();
foreach ( $your_list_of_blog_ids as $blog_id ) {
    switch_to_blog( $blog_id );
    $query = new WP_Query(
        array(
            'post_type' => 'any',
            'posts_per_page' => 10,
        )
    );
    while ( $query->have_posts() ) {
        $query->next_post();
        $posts[] = $query->post;
    }
    restore_current_blog();
}

Important are switch_to_blog and restore_current_blog. The rest of the code is just there to illustrate the idea.

share|improve this answer

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.