Skip to content

Instantly share code, notes, and snippets.

@urre
Created January 9, 2015 20:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save urre/44f6c6d87cc3c201122f to your computer and use it in GitHub Desktop.
Save urre/44f6c6d87cc3c201122f to your computer and use it in GitHub Desktop.
WordPress: Get posts from multisite sites (not main site)
<?php
$sites = wp_get_sites();
foreach($sites as $site) :
// Only subsites
if (!is_main_site($site['blog_id'])) :
// Connect to new multisite
switch_to_blog($site['blog_id']);
$case_args = array(
'post_type' => 'case',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
);
$latest_cases = new WP_Query($case_args);
if ( $latest_cases->have_posts() ) : ?>
<?php while ( $latest_cases->have_posts() ) : $latest_cases->the_post(); $count++;
if ( has_post_thumbnail() ) :
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large' );
$featured_image_url = $featured_image['0'];
endif;
?>
<a href="<?php the_permalink(); ?>">
<figure style="background-image: url('<?php echo $featured_image_url; ?>')"></figure>
<div class="case__slider__text">
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(); ?></p>
</div>
</a>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query();
// Quit multisite connection
restore_current_blog();
endif;
endforeach; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment