Get Recent Posts by Date in Multisite

Everything in explanation is about CDTuts

I’ve created a WordPress Network and I want is to get the recent posts from all of the selected subdomain blogs. I need to get the latest All organized by date and not organized by blog ID.

I don’t want to use switch_to_blog thus, using foreach, because I’m only looping posts within the next blogs I switch. I want to actually combine the posts and have them all organized by date. I’d figure you’d have to do this by querying mysql instead of using WP_Query. But I was hoping for a more native way of doing this. This is for the list of posts to be made at the bottom of the front page as you can see with timestamps that aren’t ascended or descended by the number greater or lesser.

I even tried doing something like this, but this wouldn’t work either. Sorting the post by Unix timestamp, to only realize that the posts are still being organized by switch_to_blog instead of a timely manner.

<?php           
    switch_to_blog(1);
    $main_posts = get_posts();
    switch_to_blog(2);
    $php_posts = get_posts();
    switch_to_blog(3);
    $wp_posts = get_posts();
    switch_to_blog(4);
    $mac_posts = get_posts();
    switch_to_blog(4);
    $psd_posts = get_posts();
    switch_to_blog(1);

    $posts = array_merge($main_posts, $php_posts, $wp_posts, $mac_posts, $psd_posts);
    usort($post, get_post_time);

    foreach($posts as $post){
    ?>
        <li class="thumb"><a href="https://wordpress.stackexchange.com/questions/116814/<?php echo the_permalink(); ?>">
            <div class="site-name">
            </div>
            <div class="title">
                <?php echo the_title(); ?><br> <?php echo get_post_time(); ?>
            </div>
        </a></li>
    <?php }         
?>

I’ve also created a function where I can select the ID of the blog using switch_to_blog. This will only work on the tiles at the top of the front page. But I’m also trying to do the same for the latest posts at the bottom. I won’t be able to create an offset so that I can keep the tile design while maintaining the 6 latest posts.

This was based on a previous answer on StackExchange

function global_latest_post($LatestBlogNumber,$LatestPostNumber, $LatestThumbSize, $LatestThumbNumber) {
    $original_blog_id = get_current_blog_id();
    $bids = array($LatestBlogNumber);    

    foreach($bids as $bid) {
        switch_to_blog($bid);
        $tiles = new WP_Query('posts_per_page=1');
        while ($tiles->have_posts()) : $tiles->the_post(); ?>
            <a href="https://wordpress.stackexchange.com/questions/116814/<?php echo the_permalink(); ?>" class="<?php echo $LatestThumbSize; ?> thumb-<?php echo $LatestThumbNumber; ?>">
                <p class="cover">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
                <p class="bold bottom">Read More &rarr;</p>
                <h1><?php echo $LatestPostNumber; ?>. <?php echo the_title(); ?> in <?php bloginfo('name'); ?></h1>
            </a>
        <?php           
        endwhile;
    }
    switch_to_blog( $original_blog_id ); //switched back to current blog
}

3 Answers
3

Since wordpress multisite uses different tables for all blogs, it is very inefficient to get all recent articles of all blogs (content aggregation) on display time since you have to query all blogs, sort the articles by date and display the ammount you need.

Plugins like WordPress Post Indexer (https://premium.wpmudev.org/project/post-indexer) additional write all posts into one central db table in the moment you insert or update them. From there, you can query them very efficiently.

I used the mentioned plugin by myself – its worth to try.
I’m not associated with the creator.

There are some things to consider too: For example: if you want display custom post meta fields, you’ll have to use switch_to_blog.

Leave a Comment