How to show random posts from a multi-site blog on main blog?

So i have a MU WP site with pro sites plugin. Everyone who signs up gets a sub-domain url and blog on that domain. They can select one of three template choices. Once these accounts are active and have blog articles on them, I’d like to show RANDOM blog post from all of the sub-domain websites that have signed up ON my main website. Hope this makes sense, please advise on how I can achieve this.

  • Vinod Dalvi

    Hi @Still_(o)-(o)_Awake,

    Thank you for your question.

    You can use the following code in template file of your child theme to display random blog posts from your multi-site.

    $your_list_of_blog_ids = wp_get_sites();
    foreach ( $your_list_of_blog_ids as $blog_id ) {
    
        switch_to_blog($blog_id['blog_id']); //switched to blog
    
        // Get latest Post
        $random_posts = get_posts('numberposts=2&orderby=rand');
    ?>
        <ul>
        <?php foreach($random_posts as $post) : setup_postdata($post);?>
            <li>
                <a href="<?php echo get_page_link($post->ID); ?>" title="<?php echo $post->post_title; ?>"><?php echo $post->post_title; ?></a>
            </li>
        <?php endforeach ; ?>
    
    <?php restore_current_blog(); //switched back to main site 
    
    }

    Alternatively you can use the following plugin and customize it to display random posts.

    https://premium.wpmudev.org/project/recent-posts

    Kind Regards,
    Vinod Dalvi

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.