How can I display posts from the other sites in a WordPress multisite setup?

JGDev picture JGDev · Oct 7, 2010 · Viewed 14.7k times · Source

I have a small network of sites setup with WordPress 3.0's multisite feature. I would like to create another site which pulls certain posts from the various other sites to display. This new 'hub' site would seem like its own separate site to the user (with domain mapping), but its content is coming from the posts from the other sites.

How can I get posts from another site in a WordPress multisite setup? Can I query for posts based on the name of the site? The end result needs to be a collection of posts from the different sites sorted by date.

Thanks for your help.

Answer

Oudin picture Oudin · Jul 9, 2012

I had the similar issue where I wanted to get posts from one blog and display it on an other I came up with the following solution which you could modify slightly to meet your needs if needed

<?php
global $switched;
switch_to_blog(2); //switched to 2

// Get latest Post
$latest_posts = get_posts('category=-3&numberposts=6&orderby=post_name&order=DSC');
$cnt =0;?> 
<ul>
    <?php foreach($latest_posts as $post) : setup_postdata($post);?>
    <li>
        <a href="<?php echo get_page_link($post->ID); ?>" title="<?php echo $post->post_title; ?>"><?php echo  short_title('...', 7); ?></a>
    </li>                                
    <?php endforeach ; ?>

<?php restore_current_blog(); //switched back to main site ?>

I'm also limiting the amount of words which is being out putted if you do not want this feature simple use

$post->post_title; 

Hope it helps.