Question

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="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></h3>
    
      <?php endforeach;
    
      restore_current_blog();
      }
    
Was it helpful?

Solution

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.

OTHER TIPS

Ricks Answer is surely helpful but I wanted to share my approach, which is an adoption or extension of your code:

First get a list of selected blogs in your network.:

$args = array('site__in' => array(2, 3, 6))
$sitesObj = get_sites($args);
$sites = object_to_array($sitesObj);

You can also exclude sites by using 'site__not_in' in the arguments of get_sites().

Convert the $sitesObj object into an array:

$sites = object_to_array($sitesObj);

object_to_array($object) {
  if (!is_object($object) && !is_array($object)) {
    return $object;
  }
  return array_map('object_to_array', (array) $object) ;
}

Then initialize a counter to control the total of posts to show and switch to each selected blog to fire the loop with your custom arguments:

$postCounter = 0;
$maxPosts = 5; // total number of posts to show

foreach ($sites as $site) {
  switch_to_blog($site['blog_id']);

  $args = array(
    'post_type' => 'post', // or custom post type
    'posts_per_page' => 2, // number of posts per blog
    'order' => 'DESC',
    'orderby' => 'date' // you could also use 'rand' here
  );

  $loop = new WP_Query($args);

  if ($loop->have_posts()) :
    while ($loop->have_posts() && $counter < $maxPosts) : $loop->the_post();
      // your output
    endwhile;
  endif;

  restore_current_blog();
}

I hope that helps :)

This

function wolpostcount_shortcode($atts) {

  function object_to_array($object) {
    if (!is_object($object) && !is_array($object)) {
      return $object;
    }
    return array_map('object_to_array', (array) $object) ;
  }

  $args = array('site__in' => array(1,7,8,12,14,15,20,21,22,25,32,33,36,41,42,46,47,48,49));
  $sitesObj = get_sites($args);
  $sites = object_to_array($sitesObj);

  foreach ($sites as $site) {
    switch_to_blog($site['blog_id']);

    $postcount = wp_count_posts('post')->publish;
    $pagecount = wp_count_posts('page')->publish;
     echo 'Posts:'.$postcount.' Pages:'.$pagecount.'<br>';
    $totalpostcount = $totalpostcount + $postcount;
    $totalpagecount = $totalpagecount + $pagecount;
    restore_current_blog();
  }

  echo 'number of posts '.$totalpostcount.'<br>';
  echo 'number of pages '.$totalpagecount.'<br>';
function get_all_networks_posts($args){
   if(!$args){
      return false;
   }
   $sites = get_sites();
   $blog_posts = array();
   if($sites){
      foreach ($sites as $site) {
         switch_to_blog($site->id);
         $posts = get_posts($args);
         $blog_posts[$site->id] = $posts; // this is the above line
         //$blog_posts[] = $posts;If you do not need site ids then use this line removing just the above line
      }
   }
   restore_current_blog();
   if($blog_posts){
      return $blog_posts;
   }
}

// Print a demo
$args = arrat(
   'post_type' => 'post',
   'posts_per_page' => -1,
);
print_r(get_all_networks_posts($args));

A while ago I wrote a little internal plugin to show posts from the main site (where the blog is) on the subsite in the network. Also I wanted to only show posts from a specific category on a certain subsite.

function custom_multiblog ( $atts ) {
    // Get current blog
    $original_blog_id = get_current_blog_id();

    // Setup a category for each blog id you want to loop through - EDIT
    if ( 19 == $original_blog_id ) {
        $catslug_per_blog_id = array( 1 => 'category1', );
    } else if ( 22 == $original_blog_id ) {
        $catslug_per_blog_id = array( 1 => 'category2', );
    } else if ( 23 == $original_blog_id ) {
        $catslug_per_blog_id = array( 1 => 'category3', );
    } else {
        $catslug_per_blog_id = array( 1 => 'category1, category2, category3', );
    }
    
    foreach( $catslug_per_blog_id as $bid => $catslug ) {
        //Switch to the blog with the blog id $bid
        switch_to_blog( $bid ); 
        $args = array(
            //'post_type' => 'post',
            'category_name'  => $catslug,
            'posts_per_page' => 10,
        );
        $args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
        
        echo '<div class="multiblog-wrapper">';
            $query = new WP_Query( $args );

            while( $query->have_posts() ) : $query->the_post();
                echo '<article class="multiblog-item">';
                    echo '<div class="multiblog-img"><a href="' .get_the_permalink() . '">';
                    the_post_thumbnail ('large');
                    echo '</a></div>';
                    echo '<div class="multiblog-content">';
                        echo '<h2 style="line-height: 1em;"><a href="' .get_the_permalink() . '">' . get_the_title() . '</a></h2>';
                        echo '<p>' . get_the_excerpt() . '</p>';
                    echo '</div>';
                    echo '<div class="clearfix"></div>';
                echo '</article>';  
            endwhile;
        
        echo '</div>';
    }

    switch_to_blog( $original_blog_id ); 
    wp_reset_postdata();
    echo '<div class="clearfix"></div>';
    echo '<div class="multiblog-navigation">';
        previous_posts_link( '<< newer' );
        next_posts_link( 'older >>', $query->max_num_pages );
    echo '</div>';

}
add_shortcode( 'multiblog', 'jetzt_konf_multiblog' );
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top