Get posts from Network (Multisite)

The following code gives all posts from the network.
What I am trying to achieve :

  • Select which blogs to display (by ID)
  • Select how many post to display (My code selects how many post per blog)
  • Order by date or random

    $blogs = get_last_updated();
    
     foreach ($blogs AS $blog)    {    
    
      switch_to_blog($blog["blog_id"]);
      $lastposts = get_posts('numberposts=3');
    
      foreach($lastposts as $post) : ?>
    
        <a href="https://wordpress.stackexchange.com/questions/257024/<?php echo get_permalink(); ?>"><?php the_title(); ?></a></h3>
    
      <?php endforeach;
    
      restore_current_blog();
      }
    

5 s
5

I created a plugin which does something similar (called Multisite Post Display https://wordpress.org/plugins/multisite-post-reader/ ) . It displays posts from all multisite sub-sites.

The code in there might be helpful for what you are doing. You are welcome to dig into it and use the code to help with your project. (After all, I used other people’s code snippets to develop it.)

I wrote it after I did the Multisite Media Display, since I wanted a way to display media from subsites on one page, and couldn’t find any plugin that did that. Both have been useful to monitor posted media and content from my multisite.

Free, open source, and all that. Hope it is helpful.

Leave a Comment