Get posts from the Main site to sub site – WordPress MultiSites Network

I use this codes to loop over the posts in the Main site:

<?php
$sites = wp_get_sites();
foreach($sites as $site) :
    // Only subsites
        // Connect to new multisite
        switch_to_blog($site['blog_id']);
        $case_args = array(
            'post_type'        => 'downloads',
            'posts_per_page'   => 1,
            'orderby'          => 'date',
            'order'            => 'DESC',
            );
        $latest_cases = new WP_Query($case_args);
        if ( $latest_cases->have_posts() ) : ?>

<?php the_title(); ?>

<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query();
// Quit multisite connection
restore_current_blog();
endforeach; 
?>

Leave a Comment